/
NodeConcurrencyFilterTest.java
195 lines (150 loc) · 6.56 KB
/
NodeConcurrencyFilterTest.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
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.web;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.test.AbstractDatabaseTest;
import org.jumpmind.symmetric.transport.IConcurrentConnectionManager;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
public class NodeConcurrencyFilterTest extends AbstractDatabaseTest {
public NodeConcurrencyFilterTest() throws Exception {
super();
}
public NodeConcurrencyFilterTest(String dbName) {
super(dbName);
}
@Test(timeout = 60000)
public void testPullConcurrency() throws Exception {
IParameterService parameterService = getParameterService();
parameterService.saveParameter(ParameterConstants.CONCURRENT_WORKERS, 3);
NodeConcurrencyFilter filter = (NodeConcurrencyFilter) find(Constants.NODE_CONCURRENCY_FILTER);
MockWorker one = new MockWorker("00001", filter, "pull", "GET");
MockWorker two = new MockWorker("00002", filter, "pull", "GET");
MockWorker three = new MockWorker("00003", filter, "pull", "GET");
MockWorker four = new MockWorker("00004", filter, "pull", "GET");
one.start();
Thread.sleep(500);
two.start();
Thread.sleep(500);
three.start();
Thread.sleep(500);
four.start();
Thread.sleep(500);
Assert.assertEquals(one.reached, true);
Assert.assertEquals(two.reached, true);
Assert.assertEquals(three.reached, true);
Assert.assertEquals(four.reached, false);
one.hold = false;
two.hold = false;
three.hold = false;
four.hold = false;
Thread.sleep(500);
Assert.assertEquals(one.success, true);
Assert.assertEquals(two.success, true);
Assert.assertEquals(three.success, true);
Assert.assertEquals(four.success, false);
MockWorker five = new MockWorker("00005", filter, "pull", "GET");
five.hold = false;
five.start();
Thread.sleep(500);
Assert.assertEquals(five.success, true);
}
@Test(timeout = 60000)
public void testPushConcurrency() throws Exception {
IParameterService parameterService = getParameterService();
parameterService.saveParameter(ParameterConstants.CONCURRENT_WORKERS, 2);
NodeConcurrencyFilter filter = (NodeConcurrencyFilter) find(Constants.NODE_CONCURRENCY_FILTER);
IConcurrentConnectionManager manager = (IConcurrentConnectionManager) find(Constants.CONCURRENT_CONNECTION_MANGER);
MockWorker one = new MockWorker("00001", filter, "push", "HEAD");
MockWorker two = new MockWorker("00002", filter, "push", "HEAD");
one.start();
two.start();
Thread.sleep(500);
Assert.assertEquals(manager.getReservationCount("/sync/push"), 2);
one = new MockWorker("00001", filter, "push", "PUT");
two = new MockWorker("00002", filter, "push", "PUT");
one.start();
two.start();
Thread.sleep(500);
Assert.assertEquals(one.reached, true);
Assert.assertEquals(two.reached, true);
Assert.assertEquals(manager.getReservationCount("/sync/push"), 2);
MockWorker five = new MockWorker("00005", filter, "push", "PUT");
five.hold = false;
five.start();
Thread.sleep(500);
Assert.assertEquals(five.reached, false);
Assert.assertEquals(manager.getReservationCount("/sync/push"), 2);
one.hold = false;
two.hold = false;
Thread.sleep(500);
Assert.assertEquals(manager.getReservationCount("/sync/push"), 0);
}
class MockWorker extends Thread {
private String servletPath;
private String httpMethod;
Exception inError;
NodeConcurrencyFilter filter;
String nodeId;
boolean success = false;
boolean hold = true;
boolean reached = false;
MockWorker(String nodeId, NodeConcurrencyFilter filter, String path, String httpMethod) {
this.setDaemon(true);
this.nodeId = nodeId;
this.filter = filter;
this.httpMethod = httpMethod;
this.servletPath = path;
}
public void run() {
MockHttpServletRequest req = new MockHttpServletRequest(httpMethod, "/sync/" + servletPath);
req.addParameter(WebConstants.NODE_ID, nodeId);
req.setServletPath(servletPath);
HttpServletResponse resp = new MockHttpServletResponse();
try {
filter.doFilter(req, resp, new FilterChain() {
public void doFilter(ServletRequest request, ServletResponse response) throws IOException,
ServletException {
reached = true;
while (hold) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
success = true;
}
});
} catch (Exception e) {
this.inError = e;
}
}
}
}