blob: 322bc81a049c5d4289b5c0548eccb248f26d6421 (
plain)
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
|
package com.vaadin.ui;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.ServletConfig;
import org.junit.Assert;
import org.junit.Test;
import com.vaadin.server.DefaultDeploymentConfiguration;
import com.vaadin.server.MockServletConfig;
import com.vaadin.server.MockVaadinSession;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.VaadinServletService;
import com.vaadin.server.VaadinSession;
import com.vaadin.server.communication.PushConnection;
import com.vaadin.shared.communication.PushMode;
public class UITest {
@Test
public void removeFromSessionWithExternalLock() throws Exception {
// See https://dev.vaadin.com/ticket/18436
final UI ui = new UI() {
@Override
protected void init(VaadinRequest request) {
}
};
final Lock externalLock = new ReentrantLock();
ServletConfig servletConfig = new MockServletConfig();
VaadinServlet servlet = new VaadinServlet();
servlet.init(servletConfig);
DefaultDeploymentConfiguration deploymentConfiguration = new DefaultDeploymentConfiguration(
UI.class, new Properties());
MockVaadinSession session = new MockVaadinSession(
new VaadinServletService(servlet, deploymentConfiguration));
session.lock();
ui.setSession(session);
ui.getPushConfiguration().setPushMode(PushMode.MANUAL);
ui.setPushConnection(new PushConnection() {
private boolean connected = true;
@Override
public void push() {
}
@Override
public boolean isConnected() {
return connected;
}
@Override
public void disconnect() {
externalLock.lock();
try {
connected = false;
} finally {
externalLock.unlock();
}
}
});
session.unlock();
final CountDownLatch websocketReachedCheckpoint = new CountDownLatch(1);
final CountDownLatch uiDisconnectReachedCheckpoint = new CountDownLatch(
1);
final VaadinSession uiSession = ui.getSession();
final ConcurrentLinkedQueue<Exception> exceptions = new ConcurrentLinkedQueue<Exception>();
// Simulates the websocket close thread
Runnable websocketClose = new Runnable() {
@Override
public void run() {
externalLock.lock();
// Wait for disconnect thread to lock VaadinSession
websocketReachedCheckpoint.countDown();
try {
uiDisconnectReachedCheckpoint.await();
} catch (InterruptedException e) {
e.printStackTrace();
exceptions.add(e);
return;
}
uiSession.lock();
externalLock.unlock();
}
};
Runnable disconnectPushFromUI = new Runnable() {
@Override
public void run() {
uiSession.lock();
// Wait for websocket thread to lock external lock
uiDisconnectReachedCheckpoint.countDown();
try {
websocketReachedCheckpoint.await();
} catch (InterruptedException e) {
e.printStackTrace();
exceptions.add(e);
return;
}
ui.setSession(null);
uiSession.unlock();
}
};
Thread websocketThread = new Thread(websocketClose);
websocketThread.start();
Thread uiDisconnectThread = new Thread(disconnectPushFromUI);
uiDisconnectThread.start();
websocketThread.join(5000);
uiDisconnectThread.join(5000);
if (websocketThread.isAlive() || uiDisconnectThread.isAlive()) {
websocketThread.interrupt();
uiDisconnectThread.interrupt();
Assert.fail("Threads are still running");
}
if (!exceptions.isEmpty()) {
for (Exception e : exceptions) {
e.printStackTrace();
}
Assert.fail("There were exceptions in the threads");
}
Assert.assertNull(ui.getSession());
// PushConnection is set to null in another thread. We need to wait for
// that to happen
for (int i = 0; i < 10; i++) {
if (ui.getPushConnection() == null) {
break;
}
Thread.sleep(500);
}
Assert.assertNull(ui.getPushConnection());
}
}
|