-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
TestFBEventManager.java
278 lines (231 loc) · 9.17 KB
/
TestFBEventManager.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package org.firebirdsql.event;
import java.io.File;
import java.sql.*;
import org.firebirdsql.common.FBTestBase;
import org.firebirdsql.gds.impl.GDSType;
import static org.firebirdsql.common.SimpleFBTestBase.*;
/**
* Test the FBEventManager class
*/
public class TestFBEventManager extends FBTestBase {
private EventManager eventManager;
private boolean eventManagerDisconnected;
public static final String TABLE_DEF = ""
+ "CREATE TABLE TEST ("
+ " TESTVAL INTEGER NOT NULL"
+ ")";
public static final String TRIGGER_DEF = ""
+ "CREATE TRIGGER INSERT_TRIG "
+ " FOR TEST AFTER INSERT "
+ "AS BEGIN "
+ " POST_EVENT 'TEST_EVENT_A';"
+ " POST_EVENT 'TEST_EVENT_B';"
+ " POST_EVENT 'TEST_EVENT_A';"
+ "END";
// Delay to wait after registering an event listener before testing
static final int DELAY = 500;
public TestFBEventManager(String name) throws Exception {
super(name);
Class.forName("org.firebirdsql.jdbc.FBDriver");
}
protected void setUp() throws Exception {
super.setUp();
executeSql(TABLE_DEF);
executeSql(TRIGGER_DEF);
eventManager = new FBEventManager(getGdsType());
if (getGdsType() == GDSType.getType("PURE_JAVA") || getGdsType() == GDSType.getType("NATIVE")) {
eventManager.setHost(DB_SERVER_URL);
}
eventManager.setUser(DB_USER);
eventManager.setPassword(DB_PASSWORD);
// have to resolve relative path to the absolute one
File tempFile = new File(getDatabasePath());
eventManager.setDatabase(tempFile.getAbsolutePath());
eventManager.connect();
eventManagerDisconnected = false;
}
private void executeSql(String sql) throws SQLException {
Connection conn = getConnectionViaDriverManager();
try {
Statement stmt = conn.createStatement();
stmt.execute(sql);
} finally {
conn.close();
}
}
protected void tearDown() throws Exception {
if (!eventManagerDisconnected)
eventManager.disconnect();
eventManagerDisconnected = true;
super.tearDown();
}
public void testWaitForEventNoEvent() throws Exception {
assertEquals(-1, eventManager.waitForEvent("TEST_EVENT_B", 500));
}
public void testWaitForEventIndefinitely() throws Exception {
EventWaitThread waitThread = new EventWaitThread("TEST_EVENT_B", 0);
waitThread.start();
Thread.sleep(1000);
waitThread.interrupt();
assertEquals(0, waitThread.getEventCount());
}
public void testWaitForEventWithOccurrence() throws Exception {
EventWaitThread waitThread =
new EventWaitThread("TEST_EVENT_B", 10000);
waitThread.start();
Thread.sleep(DELAY);
executeSql("INSERT INTO TEST VALUES (1)");
waitThread.join();
assertEquals(1, waitThread.getEventCount());
}
public void testWaitForEventWithOccurrenceNoTimeout() throws Exception {
EventWaitThread waitThread = new EventWaitThread("TEST_EVENT_A", 0);
waitThread.start();
Thread.sleep(DELAY);
executeSql("INSERT INTO TEST VALUES (2)");
waitThread.join();
assertEquals(2, waitThread.getEventCount());
}
public void testBasicEventMechanism() throws Exception {
AccumulatingEventListener ael = new AccumulatingEventListener();
eventManager.addEventListener("TEST_EVENT_B", ael);
Thread.sleep(DELAY);
executeSql("INSERT INTO TEST VALUES (2)");
Thread.sleep(DELAY);
eventManager.removeEventListener("TEST_EVENT_B", ael);
int totalEvents = ael.getTotalEvents();
assertEquals("Assert that all events were recorded", 1, totalEvents);
executeSql("INSERT INTO TEST VALUES (3)");
assertEquals("No notification for events after removal of listener",
totalEvents, ael.getTotalEvents());
}
public void testAsyncEventsNoEvents() throws Exception {
Thread.sleep(DELAY);
try {
eventManager.disconnect();
} finally {
eventManagerDisconnected = true;
}
}
public void testMultipleListenersOnOneEvent() throws Exception {
AccumulatingEventListener ael1 = new AccumulatingEventListener();
AccumulatingEventListener ael2 = new AccumulatingEventListener();
AccumulatingEventListener ael3 = new AccumulatingEventListener();
eventManager.addEventListener("TEST_EVENT_B", ael1);
eventManager.addEventListener("TEST_EVENT_B", ael2);
eventManager.addEventListener("NOT_REAL_EVENT", ael3);
Thread.sleep(DELAY);
executeSql("INSERT INTO TEST VALUES (4)");
Thread.sleep(DELAY);
eventManager.removeEventListener("TEST_EVENT_B", ael1);
executeSql("INSERT INTO TEST VALUES (5)");
Thread.sleep(DELAY);
assertEquals(1, ael1.getTotalEvents());
assertEquals(2, ael2.getTotalEvents());
assertEquals(0, ael3.getTotalEvents());
}
public void testLargeMultiLoad() throws Exception {
final int THREAD_COUNT = 5;
final int REP_COUNT = 100;
Thread[] producerThreads = new Thread[THREAD_COUNT];
AccumulatingEventListener ael1 = new AccumulatingEventListener();
AccumulatingEventListener ael2 = new AccumulatingEventListener();
AccumulatingEventListener ael3 = new AccumulatingEventListener();
eventManager.addEventListener("TEST_EVENT_A", ael1);
eventManager.addEventListener("TEST_EVENT_B", ael2);
eventManager.addEventListener("TEST_EVENT_A", ael3);
eventManager.addEventListener("TEST_EVENT_B", ael3);
Thread.sleep(DELAY);
for (int i = 0; i < THREAD_COUNT; i++){
Thread t = new EventProducerThread(REP_COUNT);
producerThreads[i] = t;
t.start();
}
for (int i = 0; i < THREAD_COUNT; i++){
Thread t = producerThreads[i];
t.join();
}
Thread.sleep(DELAY);
eventManager.removeEventListener("TEST_EVENT_A", ael1);
eventManager.removeEventListener("TEST_EVENT_B", ael2);
eventManager.removeEventListener("TEST_EVENT_A", ael3);
eventManager.removeEventListener("TEST_EVENT_B", ael3);
assertEquals(THREAD_COUNT * REP_COUNT * 2, ael1.getTotalEvents());
assertEquals(THREAD_COUNT * REP_COUNT, ael2.getTotalEvents());
assertEquals(THREAD_COUNT * REP_COUNT * 3, ael3.getTotalEvents());
}
public void testSlowCallback() throws Exception {
final int REP_COUNT = 5;
AccumulatingEventListener ael = new AccumulatingEventListener(){
public void eventOccurred(DatabaseEvent e) {
try {
Thread.sleep(300);
} catch (InterruptedException ie){
}
super.eventOccurred(e);
}
};
eventManager.addEventListener("TEST_EVENT_B", ael);
Thread.sleep(DELAY);
for (int i = 0; i < REP_COUNT; i++){
executeSql("INSERT INTO TEST VALUES (5)");
}
Thread.sleep(REP_COUNT * 300);
eventManager.removeEventListener("TEST_EVENT_B", ael);
assertEquals(REP_COUNT, ael.getTotalEvents());
}
class EventWaitThread extends Thread {
private String eventName;
private int eventCount;
private int timeout;
public EventWaitThread(String eventName, int timeout){
this.eventName = eventName;
this.timeout = timeout;
}
public void run(){
try {
eventCount = eventManager.waitForEvent(eventName, timeout);
} catch (InterruptedException ie){
ie.printStackTrace();
eventCount = 0;
} catch (Exception e){
throw new RuntimeException(e.getMessage());
}
}
public int getEventCount(){
return this.eventCount;
}
}
class AccumulatingEventListener implements EventListener {
private int eventCount = 0;
public int getTotalEvents(){
return eventCount;
}
public void eventOccurred(DatabaseEvent event){
eventCount += event.getEventCount();
}
}
class EventProducerThread extends Thread {
private int count;
public EventProducerThread(int count){
this.count = count;
}
public void run(){
try {
Connection conn = TestFBEventManager.this.getConnectionViaDriverManager();
PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO TEST VALUES (?)");
try {
for (int i = 0; i < count; i++){
stmt.setInt(1, i);
stmt.execute();
}
} finally {
conn.close();
}
} catch (Exception e){
throw new RuntimeException(e.getMessage());
}
}
}
}