Skip to content

Commit 5299571

Browse files
Removing log dependency from ThreadingAndWaitlist test (#307)
* log dependency for the test is removed * threads manipulation by setting names
1 parent e429cc6 commit 5299571

File tree

5 files changed

+54
-55
lines changed

5 files changed

+54
-55
lines changed

src/functionaltests/java/com/ericsson/ei/threadingAndWaitlistRepeat/ThreadingAndWaitlistRepeatSteps.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotEquals;
55

6-
import java.io.File;
7-
import java.io.IOException;
8-
import java.io.PrintStream;
9-
import java.nio.file.Files;
106
import java.util.ArrayList;
117
import java.util.List;
8+
import java.util.Set;
129
import java.util.concurrent.TimeUnit;
1310
import java.util.regex.Matcher;
1411
import java.util.regex.Pattern;
@@ -19,10 +16,12 @@
1916
import org.springframework.core.env.Environment;
2017
import org.springframework.test.context.TestPropertySource;
2118

19+
import com.ericsson.ei.handlers.EventToObjectMapHandler;
20+
import com.ericsson.ei.rules.RulesObject;
2221
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;
2424

25-
import cucumber.api.java.Before;
2625
import cucumber.api.java.en.Given;
2726
import cucumber.api.java.en.Then;
2827

@@ -36,14 +35,16 @@
3635
"spring.data.mongodb.database: ThreadingAndWaitlistRepeatSteps",
3736
"missedNotificationDataBaseName: ThreadingAndWaitlistRepeatSteps-missedNotifications",
3837
"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" })
4140

4241
@Ignore
4342
public class ThreadingAndWaitlistRepeatSteps extends FunctionalTestBase {
44-
45-
private File tempLogFile;
4643
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;
4748

4849
@Value("${threads.corePoolSize}")
4950
private int corePoolSize;
@@ -54,18 +55,12 @@ public class ThreadingAndWaitlistRepeatSteps extends FunctionalTestBase {
5455
@Value("${waitlist.collection.ttlValue}")
5556
private int waitlistTtl;
5657

57-
@Autowired
58-
WaitListWorker waitListWorker;
58+
private RulesObject rulesObject;
5959

60-
@Autowired
61-
Environment environment;
60+
private JsonNode rulesJson;
6261

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;
6964

7065
@Given("^that eiffel events are sent$")
7166
public void that_eiffel_events_are_sent() throws Throwable {
@@ -86,45 +81,49 @@ public void no_event_is_aggregated() throws Throwable {
8681
assertEquals("aggregatedObjectExists was true, should be false, ", false, aggregatedObjectExists);
8782
}
8883

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);
10596
}
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());
107107
}
108108

109109
@Then("^correct amount of threads should be spawned$")
110110
public void correct_amount_of_threads_should_be_spawned() throws Throwable {
111+
List<String> threadsSpawned = new ArrayList<>();
111112
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));
123123
}
124124
}
125125
}
126-
127-
assertEquals(getEventNamesToSend().size() - queueCapacity, threadIds.size());
126+
assertEquals(getEventNamesToSend().size() - queueCapacity, threadsSpawned.size());
128127
}
129128

130129
@Then("^after the time to live has ended, the waitlist should be empty$")

src/functionaltests/resources/features/threadingAndWaitlistRepeat.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Feature: Test Threading and Waitlist Repeat
66
Given that eiffel events are sent
77
Then waitlist should not be empty
88
And no event is aggregated
9-
And the waitlist will try to resend the events at given time interval
9+
And event-to-object-map is manipulated to include the sent events
10+
And when waitlist has resent events they should have been deleted
1011
And correct amount of threads should be spawned
1112
And after the time to live has ended, the waitlist should be empty

src/main/java/com/ericsson/ei/handlers/EventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public RulesHandler getRulesHandler() {
5454
public void eventReceived(String event) {
5555
RulesObject eventRules = rulesHandler.getRulesForEvent(event);
5656
idRulesHandler.runIdRules(eventRules, event);
57-
// downstreamIdRulesHandler.runIdRules(eventRules, event);
5857
}
5958

6059
@Async
@@ -64,6 +63,7 @@ public void onMessage(Message message, Channel channel) throws Exception {
6463
JsonNode node = objectMapper.readTree(messageBody);
6564
String id = node.get("meta").get("id").toString();
6665
String port = environment.getProperty("local.server.port");
66+
Thread.currentThread().setName(Thread.currentThread().getName() + "-" + port);
6767
LOGGER.debug("Thread id {} spawned for EventHandler on port: {}", Thread.currentThread().getId(), port);
6868
LOGGER.debug("Event {} received on port {}", id, port);
6969

src/main/java/com/ericsson/ei/waitlist/WaitListWorker.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ public void run() {
7070
checkTargetAggregationsExistAndRepublishEvent(eventJson);
7171
}
7272
} catch (Exception e) {
73-
// LOGGER.error("Exception occured while trying to resend event: " + document,
74-
// e);
7573
LOGGER.error("Exception occured while trying to resend event: {}", document, e);
7674
}
7775
}

src/test/java/com/ericsson/ei/flowtests/TrafficGeneratedTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public void flowTest() {
155155
* are related. Events are sent to RabbitMQ queue. Deterministic traffic is
156156
* used.
157157
*
158-
* @param eventNames list of events to be sent.
158+
* @param eventNames
159+
* list of events to be sent.
159160
* @return list of ready to send events.
160161
*/
161162
private List<String> getPreparedEventsToSend(List<String> eventNames) throws IOException {

0 commit comments

Comments
 (0)