1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.daemon;
35
36 import java.util.concurrent.BrokenBarrierException;
37 import java.util.concurrent.TimeoutException;
38
39 import fr.paris.lutece.portal.service.datastore.DatastoreService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.test.LuteceTestCase;
42
43 public class AppDaemonServiceConcurrentTest extends LuteceTestCase
44 {
45 private static final String INTERVAL_VALUE = "10000";
46 private static final String JUNIT_DAEMON = "JUNITAppDaemonServiceConcurrentTest";
47 private static final String JUNIT_OTHERDAEMON = "OTHERJUNITAppDaemonServiceConcurrentTest";
48 private static final String DAEMON_INTERVAL_DSKEY = "core.daemon." + JUNIT_DAEMON + ".interval";
49 private static final String OTHERDAEMON_INTERVAL_DSKEY = "core.daemon." + JUNIT_OTHERDAEMON + ".interval";
50
51 private DaemonEntry _entry;
52 private DaemonEntry _otherEntry;
53
54 @Override
55 protected void setUp( ) throws Exception
56 {
57 super.setUp( );
58 log( "Creating daemon " + JUNIT_DAEMON );
59 _entry = new DaemonEntry( );
60 _entry.setId( JUNIT_DAEMON );
61 _entry.setNameKey( JUNIT_DAEMON );
62 _entry.setDescriptionKey( JUNIT_DAEMON );
63 _entry.setClassName( TestDaemon.class.getName( ) );
64 _entry.setPluginName( "core" );
65
66
67 DatastoreService.setInstanceDataValue( DAEMON_INTERVAL_DSKEY, INTERVAL_VALUE );
68 AppDaemonService.registerDaemon( _entry );
69
70 log( "Creating daemon " + JUNIT_OTHERDAEMON );
71 _otherEntry = new DaemonEntry( );
72 _otherEntry.setId( JUNIT_OTHERDAEMON );
73 _otherEntry.setNameKey( JUNIT_OTHERDAEMON );
74 _otherEntry.setDescriptionKey( JUNIT_OTHERDAEMON );
75 _otherEntry.setClassName( TestConcurrentDaemon.class.getName( ) );
76 _otherEntry.setPluginName( "core" );
77
78
79 DatastoreService.setInstanceDataValue( OTHERDAEMON_INTERVAL_DSKEY, INTERVAL_VALUE );
80 AppDaemonService.registerDaemon( _otherEntry );
81 }
82
83 private void log( String message )
84 {
85 AppLogService.info( "{} : {}", this.getClass( ).getName( ), message );
86 }
87
88 @Override
89 protected void tearDown( ) throws Exception
90 {
91 DatastoreService.removeInstanceData( DAEMON_INTERVAL_DSKEY );
92 AppDaemonService.unregisterDaemon( JUNIT_DAEMON );
93 DatastoreService.removeInstanceData( OTHERDAEMON_INTERVAL_DSKEY );
94 AppDaemonService.unregisterDaemon( JUNIT_OTHERDAEMON );
95 super.tearDown( );
96 }
97
98 public void testConcurrentDaemonRun( )
99 {
100 assertTrue( AppDaemonService.getDaemonEntries( ).contains( _entry ) );
101 assertTrue( AppDaemonService.getDaemonEntries( ).contains( _otherEntry ) );
102
103 AppDaemonService.startDaemon( JUNIT_DAEMON );
104 AppDaemonService.startDaemon( JUNIT_OTHERDAEMON );
105
106 final TestDaemon./../../../../fr/paris/lutece/portal/service/daemon/TestDaemon.html#TestDaemon">TestDaemon daemon = (TestDaemon) _entry.getDaemon( );
107 ( (TestConcurrentDaemon) _otherEntry.getDaemon( ) ).setOther( daemon );
108
109 AppDaemonService.signalDaemon( JUNIT_DAEMON );
110 AppDaemonService.signalDaemon( JUNIT_OTHERDAEMON );
111 try
112 {
113
114 daemon.waitForCompletion( );
115 }
116 catch( InterruptedException | BrokenBarrierException | TimeoutException e )
117 {
118 fail( "The timeout indicates that the two daemons could not run simultaneously" );
119 }
120
121 }
122 }