Changeset 21 for simplerunner/src


Ignore:
Timestamp:
09/22/20 08:52:04 (4 years ago)
Author:
bart
Message:

Version 1.5.

Location:
simplerunner/src
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • simplerunner/src/main/java/geniusweb/simplerunner/BasicConnection.java

    r9 r21  
    33import java.io.IOException;
    44import java.net.URI;
     5import java.util.concurrent.ArrayBlockingQueue;
    56
    67import geniusweb.connection.ConnectionEnd;
     
    2223        // to be initialized
    2324        private Listener<OUT> handler = null;
     25        private Thread handlerThread;
     26        private ArrayBlockingQueue<OUT> messages = new ArrayBlockingQueue<OUT>(4);
    2427
    2528        /**
     
    3336                this.reference = reference;
    3437                this.uri = uri;
     38
    3539        }
    3640
     
    4751                }
    4852                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();
    4969        }
    5070
     
    5575                                        "BasicConnection has not been initialized");
    5676                }
    57                 handler.notifyChange(data);
     77                try {
     78                        messages.put(data);
     79                } catch (InterruptedException e) {
     80                        throw new IOException(e);
     81                }
    5882        }
    5983
     
    7094        @Override
    7195        public void close() {
    72 
     96                handlerThread.interrupt();
     97                handlerThread = null;
     98                handler = null;
    7399        }
    74100
  • simplerunner/src/main/java/geniusweb/simplerunner/ClassPathConnectionFactory.java

    r9 r21  
    1111import geniusweb.actions.Action;
    1212import geniusweb.actions.PartyId;
     13import geniusweb.inform.Inform;
    1314import geniusweb.party.Party;
    14 import geniusweb.party.inform.Inform;
    1515import geniusweb.protocol.partyconnection.ProtocolToPartyConn;
    1616import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
     
    2222 * <code>classpath:org/my/package/class</code>
    2323 *
    24  *
    25  *
    2624 */
    2725public class ClassPathConnectionFactory implements ProtocolToPartyConnFactory {
  • simplerunner/src/main/java/geniusweb/simplerunner/NegoRunner.java

    r18 r21  
    2929        private final ProtocolToPartyConnFactory connectionfactory;
    3030        protected final Reporter log;
     31        private boolean properlyStopped = false;
    3132        private final static ObjectMapper jackson = new ObjectMapper();
     33        private final int LOOPTIME = 200;// ms
     34        private long maxruntime;
    3235
     36        /**
     37         *
     38         * @param settings
     39         * @param connectionfactory
     40         * @param logger
     41         * @param maxruntime        limit in millisecs. Ignored if 0
     42         */
    3343        public NegoRunner(NegoSettings settings,
    34                         ProtocolToPartyConnFactory connectionfactory, Reporter logger) {
     44                        ProtocolToPartyConnFactory connectionfactory, Reporter logger,
     45                        long maxruntime) {
    3546                if (settings == null || connectionfactory == null) {
    3647                        throw new NullPointerException("Arguments must be not null");
     
    4051                this.protocol = settings.getProtocol(log);
    4152                this.connectionfactory = connectionfactory;
     53                this.maxruntime = maxruntime;
     54        }
     55
     56        public boolean isProperlyStopped() {
     57                return properlyStopped;
    4258        }
    4359
     
    4662                protocol.addListener(evt -> handle(evt));
    4763                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");
    4874        }
    4975
     
    5682
    5783        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;
    6186        }
    6287
     
    88113
    89114                NegoRunner runner = new NegoRunner(settings,
    90                                 new ClassPathConnectionFactory(), new StdOutReporter());
     115                                new ClassPathConnectionFactory(), new StdOutReporter(), 0);
    91116                runner.run();
    92117        }
    93118
    94119        private static void showusage() {
    95                 System.err.println("GeniusWeb stand-alone runner. ");
     120                System.err.println("GeniusWeb stand-alone runner.");
    96121                System.err.println("first argument should be <settings.json>.");
    97122                System.err.println(
     
    100125                                "See the settings.json example file and the GeniusWeb wiki pages. ");
    101126
     127        }
     128
     129        /**
     130         * @return protocol that runs/ran the session.
     131         */
     132        public NegoProtocol getProtocol() {
     133                return protocol;
    102134        }
    103135}
  • simplerunner/src/test/java/geniusweb/simplerunner/ClassPathConnectionFactoryTest.java

    r9 r21  
    1111import geniusweb.actions.Action;
    1212import geniusweb.connection.ConnectionEnd;
    13 import geniusweb.party.inform.Inform;
     13import geniusweb.inform.Inform;
    1414import geniusweb.references.PartyRef;
    15 import geniusweb.simplerunner.ClassPathConnectionFactory;
    1615
    1716public class ClassPathConnectionFactoryTest {
  • simplerunner/src/test/java/geniusweb/simplerunner/SessionRunnerE2ETest.java

    r10 r21  
    11package geniusweb.simplerunner;
     2
     3import static org.junit.Assert.assertTrue;
    24
    35import java.io.IOException;
     
    2729@RunWith(Parameterized.class)
    2830public class SessionRunnerE2ETest {
     31        private static final int TEST_RUNTIME = 5000;
    2932        private final ObjectMapper jackson = new ObjectMapper();
    3033        private NegoRunner runner;
     
    3740                                .asList(new Object[][] { { "src/test/resources/settings.json" },
    3841                                                { "src/test/resources/settings2.json" },
    39                                                 { "src/test/resources/shaoptoursettings.json" } });
     42                                                { "src/test/resources/shaoptoursettings.json" },
     43                                                { "src/test/resources/mopac.json" } });
    4044        }
    4145
     
    5357
    5458                runner = new NegoRunner(settings, new ClassPathConnectionFactory(),
    55                                 logger);
     59                                logger, TEST_RUNTIME);
    5660
    5761        }
     
    6468        public void runTest() throws IOException {
    6569                runner.run();
     70                assertTrue(runner.isProperlyStopped());
     71                System.out.println("Final state:\n" + runner.getProtocol().getState());
    6672        }
    6773
  • simplerunner/src/test/java/geniusweb/simplerunner/SessionRunnerTest.java

    r18 r21  
    1616import org.mockito.ArgumentCaptor;
    1717
     18import geniusweb.actions.PartyId;
    1819import geniusweb.protocol.CurrentNegoState;
    1920import geniusweb.protocol.NegoState;
     
    2930public class SessionRunnerTest {
    3031        private static final ProtocolException PROTOCOL_EXC = new ProtocolException(
    31                         "fake protocol exception", "test");
     32                        "fake protocol exception", new PartyId("test"));
    3233        private CurrentNegoState finishedEvent;
    3334        private final long NOW = 1000;
     
    4748        public void smokeTest() {
    4849                new NegoRunner(mock(SessionSettings.class),
    49                                 mock(ProtocolToPartyConnFactory.class), logger);
     50                                mock(ProtocolToPartyConnFactory.class), logger, 5000);
    5051        }
    5152
     
    5960                                ProtocolToPartyConnFactory.class);
    6061
    61                 NegoRunner runner = spy(new NegoRunner(settings, factory, logger));
     62                NegoRunner runner = spy(
     63                                new NegoRunner(settings, factory, logger, 5000));
    6264                runner.run();
    6365
     
    7577                when(settings.getProtocol(any())).thenReturn(protocol);
    7678                SessionState state = mock(SessionState.class);
    77                 when(state.getError()).thenReturn(null);
    7879                when(protocol.getState()).thenReturn(state);
    7980                Reporter logger = mock(Reporter.class);
     
    8182                ProtocolToPartyConnFactory factory = mock(
    8283                                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                                });
    8991                ArgumentCaptor<Listener> listener = ArgumentCaptor
    9092                                .forClass(Listener.class);
     
    107109                when(settings.getProtocol(any())).thenReturn(protocol);
    108110                SessionState state = mock(SessionState.class);
    109                 when(state.getError()).thenReturn(PROTOCOL_EXC);
    110111                when(protocol.getState()).thenReturn(state);
    111112                @SuppressWarnings("unchecked")
    112113                ProtocolToPartyConnFactory factory = mock(
    113114                                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                                });
    120122                ArgumentCaptor<Listener> listener = ArgumentCaptor
    121123                                .forClass(Listener.class);
     
    126128                listener.getValue().notifyChange(finishedEvent);
    127129                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
    130134        }
    131135
  • simplerunner/src/test/resources/jobs/jobs1.json

    r1 r21  
    3434                                "discreteutils": {
    3535                                        "valueUtilities": {
    36                                                 "yes":1,
    37                                                 "no":0
     36                                                "yes": 1,
     37                                                "no": 0
    3838                                        }
    3939                                }
     
    4242                                "discreteutils": {
    4343                                        "valueUtilities": {
    44                                                 "yes":1,
    45                                                 "no":0
     44                                                "yes": 1,
     45                                                "no": 0
    4646                                        }
    4747                                }
     
    5050                                "discreteutils": {
    5151                                        "valueUtilities": {
    52                                                 "low":0,
    53                                                 "medium":0.5,
    54                                                 "high":1
     52                                                "low": 0,
     53                                                "medium": 0.5,
     54                                                "high": 1
    5555                                        }
    5656                                }
     
    112112                        }
    113113                },
    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                }
    115125        }
    116126}
  • simplerunner/src/test/resources/jobs/jobs2.json

    r1 r21  
    3434                                "discreteutils": {
    3535                                        "valueUtilities": {
    36                                                 "yes":0,
    37                                                 "no":1
     36                                                "yes": 0,
     37                                                "no": 1
    3838                                        }
    3939                                }
     
    4242                                "discreteutils": {
    4343                                        "valueUtilities": {
    44                                                 "yes":0.23809523809523808,
    45                                                 "no":0.761904761904762
     44                                                "yes": 0.23809523809523808,
     45                                                "no": 0.761904761904762
    4646                                        }
    4747                                }
     
    5050                                "discreteutils": {
    5151                                        "valueUtilities": {
    52                                                 "low":1.0,
    53                                                 "medium":0.5,
    54                                                 "high":0
     52                                                "low": 1.0,
     53                                                "medium": 0.5,
     54                                                "high": 0
    5555                                        }
    5656                                }
     
    112112                        }
    113113                },
    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                }
    115125        }
    116126}
  • simplerunner/src/test/resources/shaoptoursettings.json

    r18 r21  
    3434                        {
    3535                                "ProfileList": [
    36                                         "file:src/test/resources/jobs/jobs1partial.json?partial=10",
     36                                        "file:src/test/resources/jobs/jobs1partial.json",
    3737                                        "file:src/test/resources/jobs/jobs1partial.json"
    3838                                       
     
    4141                        {
    4242                                "ProfileList": [
    43                                         "file:src/test/resources/jobs/jobs2.json?partial=15",
     43                                        "file:src/test/resources/jobs/jobs2.json",
    4444                                        "file:src/test/resources/jobs/jobs2.json"
    4545                                       
     
    5555                                        "deadlinerounds": {
    5656                                                "rounds": 10,
    57                                                 "durationms": 10000
     57                                                "durationms": 1000
    5858                                        }
    5959                                }
Note: See TracChangeset for help on using the changeset viewer.