Project

General

Profile

« Previous | Next » 

Revision a179ac89

Added by Cherian Mathew over 8 years ago

#5297 Implement monitor feedback, Add corresponding tests

#5297 Add inconsistent monitor feedback test

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/io/ImportManager.java
241 241
                        uuid,
242 242
                        1000,
243 243
                        false,
244
                        ImportManager.this);
244
                        ImportManager.this,
245
                        null);
245 246
            }
246 247
        });
247 248

  
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/AbstractUtility.java
66 66
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
67 67
import eu.etaxonomy.cdm.model.common.IEnumTerm;
68 68
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
69
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
69 70
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
70 71
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
71 72
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler;
......
420 421
            final UUID uuid,
421 422
            final int pollInterval,
422 423
            final boolean cancelable,
423
            final IPostMoniteredOperationEnabled postOp) {
424
            final IPostMoniteredOperationEnabled postOp,
425
            final IFeedbackGenerator feedbackGenerator) {
424 426

  
425 427
        try {
426 428
            // get the remoting monitor the first time to make sure that the
......
439 441
                    // run the monitor until the operation is finished
440 442
                    IRemotingProgressMonitor remotingMonitor;
441 443
                    try {
442
                        remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label, uuid, pollInterval, postOp, monitor);
443
                    } catch (InterruptedException ie) {
444
                        return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ie);
444
                        remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label,
445
                                uuid,
446
                                pollInterval,
447
                                postOp,
448
                                feedbackGenerator,
449
                                monitor);
450
                    } catch (Exception ex) {
451
                        return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex);
445 452
                    }
446 453
                    final StringBuilder reportSb = new StringBuilder();
447 454
                    // collect reports
......
469 476
                }
470 477
            };
471 478

  
472
//            job.addJobChangeListener(new JobChangeAdapter() {
473
//                @Override
474
//                public void done(IJobChangeEvent event) {
475
//                    if(event.getJob().) {
476
//                        logger.warn("in jobc change listener");
477
//                    }
478
//                }
479
//            });
480

  
481 479
            // configure the job
482 480
            job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
483 481
            job.setUser(true);
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/operation/IFeedbackGenerator.java
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.operation;
11

  
12
import java.io.Serializable;
13

  
14
/**
15
 * @author cmathew
16
 * @date 28 Oct 2015
17
 *
18
 */
19
public interface IFeedbackGenerator {
20

  
21
    public Serializable generateFeedback();
22

  
23
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/util/ProgressMonitorClientManager.java
10 10
package eu.etaxonomy.taxeditor.util;
11 11

  
12 12
import java.text.DecimalFormat;
13
import java.util.Arrays;
14
import java.util.List;
13 15
import java.util.UUID;
14 16

  
15 17
import org.apache.log4j.Logger;
......
18 20
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
19 21
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
20 22
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
23
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
21 24
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
22 25

  
23 26
/**
......
31 34
public class ProgressMonitorClientManager {
32 35
    private static final Logger logger = Logger.getLogger(ProgressMonitorClientManager.class);
33 36

  
37
    /**
38
     * Polls the progress monitor service for the progress status of a monitor
39
     * corresponding to the given uuid.
40
     *
41
     * @param label for the operation
42
     * @param uuid of the remoting monitor already started on the server
43
     * @param pollInterval in milliseconds
44
     * @param cancelable flag which determines whether the operation can be cancelled
45
     * @param postOp callback for running post operation logic
46
     * @param feedbackGenerator feedback generator corresponding to the
47
     *        'wait on feedback' request made on the remoting monitor
48
     * @param monitor to be updated
49
     * @return a final progress monitor after the operation is completed
50
     * @throws InterruptedException
51
     */
52
    public IRemotingProgressMonitor pollMonitor(final String label,
53
            final UUID uuid,
54
            final int pollInterval,
55
            final IPostMoniteredOperationEnabled postOp,
56
            IFeedbackGenerator feedbackGenerator,
57
            IProgressMonitor monitor) throws InterruptedException {
58
        return pollMonitor(label, uuid, pollInterval, postOp, Arrays.asList(feedbackGenerator), monitor);
59
    }
34 60
    /**
35 61
     * Polls the progress monitor service for the progress status of a monitor
36 62
     * corresponding to the given uuid.
......
41 67
     * @param pollInterval in milliseconds
42 68
     * @param cancelable flag which determines whether the operation can be cancelled
43 69
     * @param postOp callback for running post operation logic
70
     * @param feedbackGenerators list of feedback generators corresponding to the
71
     *        size and exact order of the 'wait on feedback' requests made on the
72
     *        remoting monitor
44 73
     * @param monitor to be updated
45 74
     * @return a final progress monitor after the operation is completed
46 75
     * @throws InterruptedException
......
49 78
            final UUID uuid,
50 79
            final int pollInterval,
51 80
            final IPostMoniteredOperationEnabled postOp,
81
            List<IFeedbackGenerator> feedbackGenerators,
52 82
            IProgressMonitor monitor) throws InterruptedException {
53 83
        IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService();
54 84
        IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
55 85
        try {
56 86
            final int START_DELAY=10;
57
            // wait about 10 seconds for the remoting monitor to be initialised (i.e. for the begin task method to be called)
87
            // wait about 10 seconds for the remoting monitor to be initialised
88
            // (i.e. for the begin task method to be called ON THE REMOTING MONITOR)
58 89
            for(int i=0;i<START_DELAY;i++) {
59 90
                Thread.sleep(1000);
60 91
                logger.info("Waiting for monitered work to start ..");
......
74 105
            int editorTotalWorkDone = 0;
75 106
            int serverTotalWorkDone = 0;
76 107
            // loop until the operation is done
108
            int feedbackCount = 0;
77 109
            while(!(remotingMonitor.isCanceled() || remotingMonitor.isFailed() || remotingMonitor.isDone())) {
78 110
                // wait for pollInterval, then
79 111
                // .... retrieve remoting monitor, then
80 112
                //      .... set client monitor info
81 113
                Thread.sleep(pollInterval);
82 114
                remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
115
                // check if remoting monitor is waiting for feedback
116
                if(remotingMonitor.isWaitingForFeedback()) {
117
                    if(feedbackGenerators != null) {
118
                        // if we have run out of feedback generators while
119
                        // the remoting monitor is waiting on feedback
120
                        // then throw exception
121
                        if(feedbackCount + 1 > feedbackGenerators.size()) {
122
                            throw new IllegalStateException("Remoting monitor waiting on feedback that does not exist");
123
                        }
124
                        progressMonitorService.setFeedback(uuid, feedbackGenerators.get(feedbackCount).generateFeedback());
125
                        feedbackCount++;
126
                    }
127
                }
83 128
                serverTotalWorkDone = (int) remotingMonitor.getWorkDone();
84
                logger.warn("Work done from start: " + serverTotalWorkDone);
129
                logger.info("Work done from start: " + serverTotalWorkDone);
85 130
                String percentage = new DecimalFormat("#.##").format(remotingMonitor.getPercentage());
131
                // set dialog text
86 132
                monitor.setTaskName(label + " " + percentage + "% done ");
87 133
                monitor.subTask(remotingMonitor.getSubTask());
88 134
                int worked = serverTotalWorkDone - editorTotalWorkDone;
......
92 138
                }
93 139
                editorTotalWorkDone = serverTotalWorkDone;
94 140
            }
141
            if(remotingMonitor.getResult() instanceof Exception) {
142
                throw new IllegalStateException((Exception)remotingMonitor.getResult());
143
            }
95 144
            return remotingMonitor;
96 145
        } finally {
97
            if(postOp != null) {
146
            if(postOp != null && remotingMonitor.isDone()) {
98 147
                postOp.postOperation(remotingMonitor);
99 148
            }
100 149
        }
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/RemotingSessionAwareTest.java
51 51
        cacher = getCacher(sessionOwner);
52 52

  
53 53
        UserDetails extraUser = null;
54

  
54 55
        try {
55 56
            extraUser = userService.loadUserByUsername(extraUsername);
56 57
        } catch (UsernameNotFoundException unfe) {
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/service/ProgressMonitorServiceTest.java
9 9
 */
10 10
package eu.etaxonomy.taxeditor.service;
11 11

  
12
import java.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.List;
12 16
import java.util.UUID;
13 17

  
14 18
import org.apache.log4j.Logger;
......
22 26
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
23 27
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitor;
24 28
import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
29
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
25 30
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
26 31
import eu.etaxonomy.taxeditor.store.CdmStore;
27 32

  
......
47 52

  
48 53
    @Test
49 54
    public void testMonitLongRunningMethod() throws InterruptedException {
50
        UUID uuid = testService.monitLongRunningMethod(null);
55
        UUID uuid = testService.monitLongRunningMethod(null, null);
51 56
        int pollInterval = 1000;
52 57
        RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
53 58
        expectedMonitor.setResult("Success");
54 59
        expectedMonitor.addReport("Report");
55 60
        expectedMonitor.done();
56
        IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
61
        CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
57 62
                uuid,
58 63
                pollInterval,
59 64
                new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
65
                (IFeedbackGenerator)null,
60 66
                new NullProgressMonitor());
61 67
    }
62 68

  
......
64 70
    public void testMonitLongRunningMethodByChangingUser() throws InterruptedException {
65 71

  
66 72
        IllegalStateException ise = new IllegalStateException("IllegalStateException");
67
        UUID uuid = testService.monitLongRunningMethod(ise);
73
        UUID uuid = testService.monitLongRunningMethod(ise, null);
68 74
        authenticateExtraUser();
69 75
        IRemotingProgressMonitor monitor = progressMonitorService.getRemotingMonitor(uuid);
70 76
        Assert.assertNull(monitor);
......
74 80
    @Test
75 81
    public void testMonitLongRunningMethodWithException() throws InterruptedException {
76 82
        IllegalStateException ise = new IllegalStateException("IllegalStateException");
77
        UUID uuid = testService.monitLongRunningMethod(ise);
83
        UUID uuid = testService.monitLongRunningMethod(ise, null);
78 84
        int pollInterval = 1000;
79 85
        RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
80 86
        expectedMonitor.setResult(ise);
81 87
        expectedMonitor.setIsFailed(true);
82 88
        expectedMonitor.done();
83
        IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
89
        CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
84 90
                uuid,
85 91
                pollInterval,
86 92
                new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
93
                (IFeedbackGenerator)null,
87 94
                new NullProgressMonitor());
88 95
    }
89 96

  
90 97
    @Test
91
    public void testMonitLongRunningMethodWithInterrupt() throws InterruptedException {
98
    public void testMonitLongRunningMethodWithInterrupt() {
92 99
        IllegalStateException ise = new IllegalStateException("Interrupted Exception");
93
        final UUID uuid = testService.monitLongRunningMethod(ise);
100
        final UUID uuid = testService.monitLongRunningMethod(ise, null);
94 101
        final int pollInterval = 1000;
95 102
        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
96 103
        expectedMonitor.setResult(ise);
......
102 109
            @Override
103 110
            public void run() {
104 111
                try {
105
                    IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
112
                    CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
106 113
                            uuid,
107 114
                            pollInterval,
108 115
                            null,
116
                            (IFeedbackGenerator)null,
109 117
                            new NullProgressMonitor());
110 118
                } catch (InterruptedException e) {
111 119

  
112 120
                }
113 121
            }
114

  
115 122
        };
116 123
        thread.start();
117 124
        while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
......
128 135
    @Test
129 136
    public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
130 137

  
131
        final UUID uuid = testService.monitLongRunningMethod(null);
138
        final UUID uuid = testService.monitLongRunningMethod(null, null);
132 139
        final int pollInterval = 1000;
133 140
        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
134 141
        expectedMonitor.setResult("Cancelled");
......
139 146
            @Override
140 147
            public void run() {
141 148
                try {
142
                    IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
149
                    CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
143 150
                            uuid,
144 151
                            pollInterval,
145 152
                            null,
153
                            (IFeedbackGenerator)null,
146 154
                            new NullProgressMonitor());
147 155
                } catch (InterruptedException e) {
148 156

  
......
159 167

  
160 168
    }
161 169

  
170
    @Test
171
    public void testMonitLongRunningMethodWithWaitForFeedback() throws InterruptedException {
172

  
173
        List<String> feedbacks = Arrays.asList("feedback1", "feedback2");
174
        List<IFeedbackGenerator> feebackGenerators = new ArrayList<IFeedbackGenerator>();
175
        final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
176
        expectedMonitor.setResult("Success");
177
        for(String feedback : feedbacks) {
178
            feebackGenerators.add(new MockFeedbackGenerator(feedback));
179
            expectedMonitor.addReport(feedback);
180
        }
181
        expectedMonitor.addReport("Report");
182
        expectedMonitor.done();
183

  
184
        final UUID uuid = testService.monitLongRunningMethod(null, feedbacks);
185
        final int pollInterval = 1000;
186

  
187
        CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
188
                uuid,
189
                pollInterval,
190
                new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
191
                feebackGenerators,
192
                new NullProgressMonitor());
193

  
194
        feebackGenerators.remove(1);
195

  
196
        final UUID newUuid = testService.monitLongRunningMethod(null, feedbacks);
197

  
198
        try {
199
            CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
200
                    newUuid,
201
                    pollInterval,
202
                    new MockPostMoniteredOperationEnabled(expectedMonitor, newUuid),
203
                    feebackGenerators,
204
                    new NullProgressMonitor());
205
            Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
206
        } catch(IllegalStateException ise) {
207

  
208
        }
209

  
210

  
211
    }
212

  
162 213

  
163 214
    class MockPostMoniteredOperationEnabled implements IPostMoniteredOperationEnabled {
164 215

  
......
188 239
            Assert.assertEquals(expectedMonitor.isDone(), monitor.isDone());
189 240
            Assert.assertTrue(!progressMonitorService.isMonitorThreadRunning(monitorUuid));
190 241
        }
242
    }
243

  
244
    class MockFeedbackGenerator implements IFeedbackGenerator {
245

  
246
        private String feedback;
247

  
248
        public MockFeedbackGenerator(String feedback) {
249
            this.feedback = feedback;
250
        }
251
        /**
252
         * {@inheritDoc}
253
         */
254
        @Override
255
        public Serializable generateFeedback() {
256
            return feedback;
257
        }
191 258

  
192 259
    }
193 260
}

Also available in: Unified diff