Changeset 21 for simplerunner
- Timestamp:
- 09/22/20 08:52:04 (4 years ago)
- Location:
- simplerunner
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
simplerunner/pom.xml
r20 r21 6 6 <groupId>geniusweb</groupId> 7 7 <artifactId>simplerunner</artifactId> 8 <version>1. 4.4</version> <!-- must equal ${geniusweb.version} -->8 <version>1.5.0</version> <!-- must equal ${geniusweb.version} --> 9 9 <packaging>jar</packaging> 10 10 … … 17 17 <passwd>${env.ARTIFACTORY_PASS}</passwd> 18 18 <jackson-2-version>2.9.10</jackson-2-version> 19 <geniusweb.version>1. 4.4</geniusweb.version>19 <geniusweb.version>1.5.0</geniusweb.version> 20 20 </properties> 21 21 -
simplerunner/src/main/java/geniusweb/simplerunner/BasicConnection.java
r9 r21 3 3 import java.io.IOException; 4 4 import java.net.URI; 5 import java.util.concurrent.ArrayBlockingQueue; 5 6 6 7 import geniusweb.connection.ConnectionEnd; … … 22 23 // to be initialized 23 24 private Listener<OUT> handler = null; 25 private Thread handlerThread; 26 private ArrayBlockingQueue<OUT> messages = new ArrayBlockingQueue<OUT>(4); 24 27 25 28 /** … … 33 36 this.reference = reference; 34 37 this.uri = uri; 38 35 39 } 36 40 … … 47 51 } 48 52 this.handler = newhandler; 53 54 handlerThread = new Thread(new Runnable() { 55 56 @Override 57 public void run() { 58 try { 59 while (true) { 60 handler.notifyChange(messages.take()); 61 } 62 } catch (InterruptedException e) { 63 System.out.println("connection handler interrupted"); 64 } 65 } 66 }); 67 68 handlerThread.start(); 49 69 } 50 70 … … 55 75 "BasicConnection has not been initialized"); 56 76 } 57 handler.notifyChange(data); 77 try { 78 messages.put(data); 79 } catch (InterruptedException e) { 80 throw new IOException(e); 81 } 58 82 } 59 83 … … 70 94 @Override 71 95 public void close() { 72 96 handlerThread.interrupt(); 97 handlerThread = null; 98 handler = null; 73 99 } 74 100 -
simplerunner/src/main/java/geniusweb/simplerunner/ClassPathConnectionFactory.java
r9 r21 11 11 import geniusweb.actions.Action; 12 12 import geniusweb.actions.PartyId; 13 import geniusweb.inform.Inform; 13 14 import geniusweb.party.Party; 14 import geniusweb.party.inform.Inform;15 15 import geniusweb.protocol.partyconnection.ProtocolToPartyConn; 16 16 import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory; … … 22 22 * <code>classpath:org/my/package/class</code> 23 23 * 24 *25 *26 24 */ 27 25 public class ClassPathConnectionFactory implements ProtocolToPartyConnFactory { -
simplerunner/src/main/java/geniusweb/simplerunner/NegoRunner.java
r18 r21 29 29 private final ProtocolToPartyConnFactory connectionfactory; 30 30 protected final Reporter log; 31 private boolean properlyStopped = false; 31 32 private final static ObjectMapper jackson = new ObjectMapper(); 33 private final int LOOPTIME = 200;// ms 34 private long maxruntime; 32 35 36 /** 37 * 38 * @param settings 39 * @param connectionfactory 40 * @param logger 41 * @param maxruntime limit in millisecs. Ignored if 0 42 */ 33 43 public NegoRunner(NegoSettings settings, 34 ProtocolToPartyConnFactory connectionfactory, Reporter logger) { 44 ProtocolToPartyConnFactory connectionfactory, Reporter logger, 45 long maxruntime) { 35 46 if (settings == null || connectionfactory == null) { 36 47 throw new NullPointerException("Arguments must be not null"); … … 40 51 this.protocol = settings.getProtocol(log); 41 52 this.connectionfactory = connectionfactory; 53 this.maxruntime = maxruntime; 54 } 55 56 public boolean isProperlyStopped() { 57 return properlyStopped; 42 58 } 43 59 … … 46 62 protocol.addListener(evt -> handle(evt)); 47 63 protocol.start(connectionfactory); 64 long remainingtime = maxruntime; 65 while (!properlyStopped && (maxruntime == 0 || remainingtime > 0)) { 66 try { 67 Thread.sleep(LOOPTIME); 68 remainingtime -= LOOPTIME; 69 } catch (InterruptedException e) { 70 e.printStackTrace(); 71 } 72 } 73 System.out.println("end run"); 48 74 } 49 75 … … 56 82 57 83 protected void stop() { 58 Level level = protocol.getState().getError() == null ? Level.INFO 59 : Level.WARNING; 60 logFinal(level, protocol.getState()); 84 logFinal(Level.INFO, protocol.getState()); 85 properlyStopped = true; 61 86 } 62 87 … … 88 113 89 114 NegoRunner runner = new NegoRunner(settings, 90 new ClassPathConnectionFactory(), new StdOutReporter() );115 new ClassPathConnectionFactory(), new StdOutReporter(), 0); 91 116 runner.run(); 92 117 } 93 118 94 119 private static void showusage() { 95 System.err.println("GeniusWeb stand-alone runner. 120 System.err.println("GeniusWeb stand-alone runner."); 96 121 System.err.println("first argument should be <settings.json>."); 97 122 System.err.println( … … 100 125 "See the settings.json example file and the GeniusWeb wiki pages. "); 101 126 127 } 128 129 /** 130 * @return protocol that runs/ran the session. 131 */ 132 public NegoProtocol getProtocol() { 133 return protocol; 102 134 } 103 135 } -
simplerunner/src/test/java/geniusweb/simplerunner/ClassPathConnectionFactoryTest.java
r9 r21 11 11 import geniusweb.actions.Action; 12 12 import geniusweb.connection.ConnectionEnd; 13 import geniusweb. party.inform.Inform;13 import geniusweb.inform.Inform; 14 14 import geniusweb.references.PartyRef; 15 import geniusweb.simplerunner.ClassPathConnectionFactory;16 15 17 16 public class ClassPathConnectionFactoryTest { -
simplerunner/src/test/java/geniusweb/simplerunner/SessionRunnerE2ETest.java
r10 r21 1 1 package geniusweb.simplerunner; 2 3 import static org.junit.Assert.assertTrue; 2 4 3 5 import java.io.IOException; … … 27 29 @RunWith(Parameterized.class) 28 30 public class SessionRunnerE2ETest { 31 private static final int TEST_RUNTIME = 5000; 29 32 private final ObjectMapper jackson = new ObjectMapper(); 30 33 private NegoRunner runner; … … 37 40 .asList(new Object[][] { { "src/test/resources/settings.json" }, 38 41 { "src/test/resources/settings2.json" }, 39 { "src/test/resources/shaoptoursettings.json" } }); 42 { "src/test/resources/shaoptoursettings.json" }, 43 { "src/test/resources/mopac.json" } }); 40 44 } 41 45 … … 53 57 54 58 runner = new NegoRunner(settings, new ClassPathConnectionFactory(), 55 logger );59 logger, TEST_RUNTIME); 56 60 57 61 } … … 64 68 public void runTest() throws IOException { 65 69 runner.run(); 70 assertTrue(runner.isProperlyStopped()); 71 System.out.println("Final state:\n" + runner.getProtocol().getState()); 66 72 } 67 73 -
simplerunner/src/test/java/geniusweb/simplerunner/SessionRunnerTest.java
r18 r21 16 16 import org.mockito.ArgumentCaptor; 17 17 18 import geniusweb.actions.PartyId; 18 19 import geniusweb.protocol.CurrentNegoState; 19 20 import geniusweb.protocol.NegoState; … … 29 30 public class SessionRunnerTest { 30 31 private static final ProtocolException PROTOCOL_EXC = new ProtocolException( 31 "fake protocol exception", "test");32 "fake protocol exception", new PartyId("test")); 32 33 private CurrentNegoState finishedEvent; 33 34 private final long NOW = 1000; … … 47 48 public void smokeTest() { 48 49 new NegoRunner(mock(SessionSettings.class), 49 mock(ProtocolToPartyConnFactory.class), logger );50 mock(ProtocolToPartyConnFactory.class), logger, 5000); 50 51 } 51 52 … … 59 60 ProtocolToPartyConnFactory.class); 60 61 61 NegoRunner runner = spy(new NegoRunner(settings, factory, logger)); 62 NegoRunner runner = spy( 63 new NegoRunner(settings, factory, logger, 5000)); 62 64 runner.run(); 63 65 … … 75 77 when(settings.getProtocol(any())).thenReturn(protocol); 76 78 SessionState state = mock(SessionState.class); 77 when(state.getError()).thenReturn(null);78 79 when(protocol.getState()).thenReturn(state); 79 80 Reporter logger = mock(Reporter.class); … … 81 82 ProtocolToPartyConnFactory factory = mock( 82 83 ProtocolToPartyConnFactory.class); 83 NegoRunner runner = spy(new NegoRunner(settings, factory, logger) { 84 @Override 85 protected void logFinal(Level level, NegoState state) { 86 log.log(level, state.toString()); 87 } 88 }); 84 NegoRunner runner = spy( 85 new NegoRunner(settings, factory, logger, 5000) { 86 @Override 87 protected void logFinal(Level level, NegoState state) { 88 log.log(level, state.toString()); 89 } 90 }); 89 91 ArgumentCaptor<Listener> listener = ArgumentCaptor 90 92 .forClass(Listener.class); … … 107 109 when(settings.getProtocol(any())).thenReturn(protocol); 108 110 SessionState state = mock(SessionState.class); 109 when(state.getError()).thenReturn(PROTOCOL_EXC);110 111 when(protocol.getState()).thenReturn(state); 111 112 @SuppressWarnings("unchecked") 112 113 ProtocolToPartyConnFactory factory = mock( 113 114 ProtocolToPartyConnFactory.class); 114 NegoRunner runner = spy(new NegoRunner(settings, factory, logger) { 115 @Override 116 protected void logFinal(Level level, NegoState state) { 117 log.log(level, state.toString()); 118 } 119 }); 115 NegoRunner runner = spy( 116 new NegoRunner(settings, factory, logger, 5000) { 117 @Override 118 protected void logFinal(Level level, NegoState state) { 119 log.log(level, state.toString()); 120 } 121 }); 120 122 ArgumentCaptor<Listener> listener = ArgumentCaptor 121 123 .forClass(Listener.class); … … 126 128 listener.getValue().notifyChange(finishedEvent); 127 129 verify(runner, times(1)).stop(); 128 // check that the protocol error is logged properly 129 verify(logger, times(1)).log(eq(Level.WARNING), any(String.class)); 130 // check final state is logged 131 String statestr = state.toString(); 132 verify(logger, times(1)).log(any(), eq(statestr)); 133 130 134 } 131 135 -
simplerunner/src/test/resources/jobs/jobs1.json
r1 r21 34 34 "discreteutils": { 35 35 "valueUtilities": { 36 "yes": 1,37 "no": 036 "yes": 1, 37 "no": 0 38 38 } 39 39 } … … 42 42 "discreteutils": { 43 43 "valueUtilities": { 44 "yes": 1,45 "no": 044 "yes": 1, 45 "no": 0 46 46 } 47 47 } … … 50 50 "discreteutils": { 51 51 "valueUtilities": { 52 "low": 0,53 "medium": 0.5,54 "high": 152 "low": 0, 53 "medium": 0.5, 54 "high": 1 55 55 } 56 56 } … … 112 112 } 113 113 }, 114 "name": "jobs1" 114 "name": "jobs1", 115 "reservationBid": { 116 "issuevalues": { 117 "salary": "3500", 118 "fte": "0.8", 119 "work from home": "1", 120 "lease car": "no", 121 "permanent contract": "yes", 122 "career development opportunities": "medium" 123 } 124 } 115 125 } 116 126 } -
simplerunner/src/test/resources/jobs/jobs2.json
r1 r21 34 34 "discreteutils": { 35 35 "valueUtilities": { 36 "yes": 0,37 "no": 136 "yes": 0, 37 "no": 1 38 38 } 39 39 } … … 42 42 "discreteutils": { 43 43 "valueUtilities": { 44 "yes": 0.23809523809523808,45 "no": 0.76190476190476244 "yes": 0.23809523809523808, 45 "no": 0.761904761904762 46 46 } 47 47 } … … 50 50 "discreteutils": { 51 51 "valueUtilities": { 52 "low": 1.0,53 "medium": 0.5,54 "high": 052 "low": 1.0, 53 "medium": 0.5, 54 "high": 0 55 55 } 56 56 } … … 112 112 } 113 113 }, 114 "name": "jobs2" 114 "name": "jobs2", 115 "reservationBid": { 116 "issuevalues": { 117 "salary": "3000", 118 "fte": "0.8", 119 "work from home": "0", 120 "lease car": "no", 121 "permanent contract": "no", 122 "career development opportunities": "medium" 123 } 124 } 115 125 } 116 126 } -
simplerunner/src/test/resources/shaoptoursettings.json
r18 r21 34 34 { 35 35 "ProfileList": [ 36 "file:src/test/resources/jobs/jobs1partial.json ?partial=10",36 "file:src/test/resources/jobs/jobs1partial.json", 37 37 "file:src/test/resources/jobs/jobs1partial.json" 38 38 … … 41 41 { 42 42 "ProfileList": [ 43 "file:src/test/resources/jobs/jobs2.json ?partial=15",43 "file:src/test/resources/jobs/jobs2.json", 44 44 "file:src/test/resources/jobs/jobs2.json" 45 45 … … 55 55 "deadlinerounds": { 56 56 "rounds": 10, 57 "durationms": 1000 057 "durationms": 1000 58 58 } 59 59 }
Note:
See TracChangeset
for help on using the changeset viewer.