3
3
import static org .junit .Assert .assertEquals ;
4
4
import static org .junit .Assert .assertNotEquals ;
5
5
6
- import java .io .File ;
7
- import java .io .IOException ;
8
- import java .io .PrintStream ;
9
- import java .nio .file .Files ;
10
6
import java .util .ArrayList ;
11
7
import java .util .List ;
8
+ import java .util .Set ;
12
9
import java .util .concurrent .TimeUnit ;
13
10
import java .util .regex .Matcher ;
14
11
import java .util .regex .Pattern ;
19
16
import org .springframework .core .env .Environment ;
20
17
import org .springframework .test .context .TestPropertySource ;
21
18
19
+ import com .ericsson .ei .handlers .EventToObjectMapHandler ;
20
+ import com .ericsson .ei .rules .RulesObject ;
22
21
import com .ericsson .ei .utils .FunctionalTestBase ;
23
- import com .ericsson .ei .waitlist .WaitListWorker ;
22
+ import com .fasterxml .jackson .databind .JsonNode ;
23
+ import com .fasterxml .jackson .databind .ObjectMapper ;
24
24
25
- import cucumber .api .java .Before ;
26
25
import cucumber .api .java .en .Given ;
27
26
import cucumber .api .java .en .Then ;
28
27
36
35
"spring.data.mongodb.database: ThreadingAndWaitlistRepeatSteps" ,
37
36
"missedNotificationDataBaseName: ThreadingAndWaitlistRepeatSteps-missedNotifications" ,
38
37
"rabbitmq.exchange.name: ThreadingAndWaitlistRepeatSteps-exchange" ,
39
- "rabbitmq.consumerName: ThreadingAndWaitlistRepeatStepsConsumer" ,
40
- "logging.level.com.ericsson.ei.waitlist=DEBUG" , "logging.level.com.ericsson.ei. handlers.EventHandler=DEBUG " })
38
+ "rabbitmq.consumerName: ThreadingAndWaitlistRepeatStepsConsumer" , "logging.level.com.ericsson.ei.waitlist=OFF" ,
39
+ "logging.level.com.ericsson.ei.handlers.EventHandler=OFF " })
41
40
42
41
@ Ignore
43
42
public class ThreadingAndWaitlistRepeatSteps extends FunctionalTestBase {
44
-
45
- private File tempLogFile ;
46
43
private static final String EIFFEL_EVENTS_JSON_PATH = "src/functionaltests/resources/eiffel_events_for_thread_testing.json" ;
44
+ private static final String ID_RULE = "{" + "\" IdRule\" : \" meta.id\" " + "}" ;
45
+
46
+ @ Autowired
47
+ private Environment environment ;
47
48
48
49
@ Value ("${threads.corePoolSize}" )
49
50
private int corePoolSize ;
@@ -54,18 +55,12 @@ public class ThreadingAndWaitlistRepeatSteps extends FunctionalTestBase {
54
55
@ Value ("${waitlist.collection.ttlValue}" )
55
56
private int waitlistTtl ;
56
57
57
- @ Autowired
58
- WaitListWorker waitListWorker ;
58
+ private RulesObject rulesObject ;
59
59
60
- @ Autowired
61
- Environment environment ;
60
+ private JsonNode rulesJson ;
62
61
63
- @ Before ("@ThreadingAndWaitlistRepeatScenario" )
64
- public void beforeScenario () throws IOException {
65
- tempLogFile = File .createTempFile ("logfile" , ".tmp" );
66
- tempLogFile .deleteOnExit ();
67
- System .setOut (new PrintStream (tempLogFile ));
68
- }
62
+ @ Autowired
63
+ EventToObjectMapHandler eventToObjectMapHanler ;
69
64
70
65
@ Given ("^that eiffel events are sent$" )
71
66
public void that_eiffel_events_are_sent () throws Throwable {
@@ -86,45 +81,49 @@ public void no_event_is_aggregated() throws Throwable {
86
81
assertEquals ("aggregatedObjectExists was true, should be false, " , false , aggregatedObjectExists );
87
82
}
88
83
89
- @ Then ("^the waitlist will try to resend the events at given time interval$" )
90
- public void the_waitlist_will_try_to_resend_the_events_at_given_time_interval () throws Throwable {
91
- TimeUnit .SECONDS .sleep (5 );
92
- List <String > resentEvents = new ArrayList <>();
93
- List <String > lines = new ArrayList <>(Files .readAllLines (tempLogFile .toPath ()));
94
-
95
- int waitlistId = waitListWorker .hashCode ();
96
- String waitListIdPattern = "FROM WAITLIST: " + waitlistId ;
97
- Pattern pattern = Pattern .compile ("\\ [EIFFEL EVENT RESENT " + waitListIdPattern + "\\ ] id:([a-zA-Z\\ d-]+)" );
98
- for (String line : lines ) {
99
- Matcher matcher = pattern .matcher (line );
100
- if (matcher .find () && !matcher .group (1 ).equals ("" )) {
101
- if (!resentEvents .contains (matcher .group (1 ))) {
102
- resentEvents .add (matcher .group (1 ));
103
- }
104
- }
84
+ @ Then ("^event-to-object-map is manipulated to include the sent events$" )
85
+ public void event_to_object_map_is_manipulated_to_include_the_sent_events () throws Throwable {
86
+ JsonNode parsedJSON = eventManager .getJSONFromFile (EIFFEL_EVENTS_JSON_PATH );
87
+ ObjectMapper objectMapper = new ObjectMapper ();
88
+ rulesJson = objectMapper .readTree (ID_RULE );
89
+ rulesObject = new RulesObject (rulesJson );
90
+
91
+ String dummyObjectID = "1234abcd-12ab-12ab-12ab-123456abcdef" ;
92
+ List <String > eventNames = getEventNamesToSend ();
93
+ for (String eventName : eventNames ) {
94
+ JsonNode eventJson = parsedJSON .get (eventName );
95
+ eventToObjectMapHanler .updateEventToObjectMapInMemoryDB (rulesObject , eventJson .toString (), dummyObjectID );
105
96
}
106
- assertEquals (getEventNamesToSend ().size (), resentEvents .size ());
97
+ }
98
+
99
+ @ Then ("^when waitlist has resent events they should have been deleted$" )
100
+ public void when_waitlist_has_resent_events_they_should_have_been_deleted () throws Throwable {
101
+ long stopTime = System .currentTimeMillis () + TimeUnit .SECONDS .toMillis (3 );
102
+ while (dbManager .waitListSize () > 0 && stopTime > System .currentTimeMillis ()) {
103
+ TimeUnit .MILLISECONDS .sleep (100 );
104
+ }
105
+ assertEquals ("Waitlist resent events and due their presence in event-to-object-map, events are deleted" , 0 ,
106
+ dbManager .waitListSize ());
107
107
}
108
108
109
109
@ Then ("^correct amount of threads should be spawned$" )
110
110
public void correct_amount_of_threads_should_be_spawned () throws Throwable {
111
+ List <String > threadsSpawned = new ArrayList <>();
111
112
String port = environment .getProperty ("local.server.port" );
112
- String eventHandlerThreadIdPattern = String .format ("Thread id (\\ d+) spawned for EventHandler on port: %s" ,
113
- port );
114
- Pattern pattern = Pattern .compile (eventHandlerThreadIdPattern );
115
- List <String > threadIds = new ArrayList <>();
116
- List <String > lines = new ArrayList <>(Files .readAllLines (tempLogFile .toPath ()));
117
-
118
- for (String line : lines ) {
119
- Matcher matcher = pattern .matcher (line );
120
- if (matcher .find () && !matcher .group (0 ).equals ("" )) {
121
- if (!threadIds .contains (matcher .group (1 ))) {
122
- threadIds .add (matcher .group (1 ));
113
+ String eventHandlerThreadNamePattern = String .format ("EventHandler-(\\ d+)-%s" , port );
114
+ Pattern pattern = Pattern .compile (eventHandlerThreadNamePattern );
115
+
116
+ Set <Thread > threadSet = Thread .getAllStackTraces ().keySet ();
117
+ Thread [] threadArray = threadSet .toArray (new Thread [threadSet .size ()]);
118
+ for (Thread thread : threadArray ) {
119
+ Matcher matcher = pattern .matcher (thread .getName ());
120
+ if (matcher .find () && !matcher .group (1 ).equals ("" )) {
121
+ if (!threadsSpawned .contains (matcher .group (1 ))) {
122
+ threadsSpawned .add (matcher .group (1 ));
123
123
}
124
124
}
125
125
}
126
-
127
- assertEquals (getEventNamesToSend ().size () - queueCapacity , threadIds .size ());
126
+ assertEquals (getEventNamesToSend ().size () - queueCapacity , threadsSpawned .size ());
128
127
}
129
128
130
129
@ Then ("^after the time to live has ended, the waitlist should be empty$" )
0 commit comments