-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainTest.java
More file actions
140 lines (117 loc) · 4.93 KB
/
Copy pathMainTest.java
File metadata and controls
140 lines (117 loc) · 4.93 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.wattics;
import com.wattics.internal.Client;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import static com.wattics.Config.DUMMY_CONFIG;
import static java.time.LocalDateTime.now;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
public class MainTest {
@Before
public void setUp() throws Exception {
ClientFactory.setInstance(new ClientFactory() {
@Override
public Client createClient() {
return new MockClient();
}
});
}
@After
public void tearDown() throws Exception {
ClientFactory.setInstance(null);
Agent.dispose();
}
@Test
public void testAllMeasurementsAreSent() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(24);
Agent agent = Agent.getInstance();
agent.addMeasurementSentHandler((measurement, closeableHttpResponse) -> {
countDownLatch.countDown();
});
ElectricityMeasurementFactory electricityMeasurementFactory = ElectricityMeasurementFactory.getInstance();
SimpleMeasurementFactory simpleMeasurementFactory = SimpleMeasurementFactory.getInstance();
List<Measurement> measurementList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
measurementList.add(electricityMeasurementFactory.build());
measurementList.add(simpleMeasurementFactory.build());
}
agent.send(measurementList, DUMMY_CONFIG);
countDownLatch.await();
}
@Test
public void testThatMeasurementsAreSortedBeforeBeingSent() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(12);
List<Measurement> sentMeasurements = new CopyOnWriteArrayList<>();
ClientFactory.setInstance(new ClientFactory() {
@Override
public Client createClient() {
return new MockClient() {
@Override
public CloseableHttpResponse send(Measurement measurement, Config config) throws IOException {
sentMeasurements.add(measurement);
countDownLatch.countDown();
return super.send(measurement, config);
}
};
}
});
Agent agent = Agent.getInstance();
SimpleMeasurementFactory simpleMeasurementFactory = SimpleMeasurementFactory.getInstance();
simpleMeasurementFactory.setId("channelId");
List<Measurement> measurementList = new ArrayList<>();
for (int i = 0; i < 12; i++) {
simpleMeasurementFactory.setTimestamp(now().minusHours(i));
measurementList.add(simpleMeasurementFactory.build());
}
agent.send(measurementList, DUMMY_CONFIG);
countDownLatch.await();
List<Measurement> sortedMeasurements = measurementList
.stream()
.sorted(comparing(measurement -> measurement.timestamp))
.collect(toList());
Assert.assertEquals(sortedMeasurements, sentMeasurements);
}
@Test
public void testProcessorIsNotIdleUntilAfterItSendsTheLastMeasurement() throws Exception {
int numberOfMeasurementsToSend = 1000;
CountDownLatch countDownLatch = new CountDownLatch(numberOfMeasurementsToSend);
ClientFactory.setInstance(new ClientFactory() {
@Override
public Client createClient() {
return new MockClient() {
@Override
public CloseableHttpResponse send(Measurement measurement, Config config) throws IOException {
if (measurement.getId().equals("0")) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
countDownLatch.countDown();
return super.send(measurement, config);
}
};
}
});
Agent agent = Agent.getInstance(2);
SimpleMeasurementFactory measurementFactory = SimpleMeasurementFactory.getInstance();
for (int i = 0; i < numberOfMeasurementsToSend; i++) {
measurementFactory.setId(Integer.toString(i % 3));
agent.send(measurementFactory.build(), DUMMY_CONFIG);
}
countDownLatch.await();
}
@Test
public void main() throws InterruptedException {
Agent agent = Agent.getInstance();
agent.send(SimpleMeasurementFactory.getInstance().build(), DUMMY_CONFIG);
}
}