File tree Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -94,14 +94,14 @@ public synchronized void open() {
94
94
95
95
@ Override
96
96
public synchronized void close () {
97
- setState (State .DISCONNECTING );
98
- if ( webSocket != null ) {
97
+ if (State .NONE != state ) {
98
+ setState ( State . DISCONNECTING );
99
99
webSocket .close (STATUS_CODE , CLOSING_MSG );
100
100
}
101
101
}
102
102
103
103
@ Override
104
- public void send (String message ) {
104
+ public synchronized void send (String message ) {
105
105
if (state == State .CONNECTED ) {
106
106
webSocket .send (message );
107
107
}
@@ -113,7 +113,9 @@ public State getState() {
113
113
}
114
114
115
115
private void setState (State newState ) {
116
- this .state = newState ;
116
+ synchronized (this ) {
117
+ this .state = newState ;
118
+ }
117
119
this .webSocketClientCallback .stateChanged ();
118
120
}
119
121
}
Original file line number Diff line number Diff line change
1
+ package com .parse ;
2
+
3
+ import junit .framework .Assert ;
4
+
5
+ import org .junit .After ;
6
+ import org .junit .Before ;
7
+ import org .junit .Test ;
8
+ import org .junit .runner .RunWith ;
9
+ import org .mockito .Mock ;
10
+ import org .mockito .runners .MockitoJUnitRunner ;
11
+
12
+ import java .net .URI ;
13
+
14
+ import okhttp3 .OkHttpClient ;
15
+
16
+ import static com .parse .WebSocketClient .State .NONE ;
17
+
18
+ @ RunWith (MockitoJUnitRunner .class )
19
+ public class TestOkHttpClientFactory {
20
+
21
+ @ Mock
22
+ private OkHttpClient okHttpClientMock ;
23
+
24
+ @ Mock
25
+ private WebSocketClient .WebSocketClientCallback webSocketClientCallbackMock ;
26
+
27
+ private OkHttp3SocketClientFactory okHttp3SocketClientFactory ;
28
+ private WebSocketClient webSocketClient ;
29
+
30
+ @ Before
31
+ public void setUp () throws Exception {
32
+ okHttp3SocketClientFactory = new OkHttp3SocketClientFactory (okHttpClientMock );
33
+ webSocketClient = okHttp3SocketClientFactory .createInstance (webSocketClientCallbackMock , new URI ("http://www.test.com" ));
34
+ }
35
+
36
+ @ After
37
+ public void tearDown () {
38
+ webSocketClient .close ();
39
+ }
40
+
41
+ @ Test
42
+ public void testClientCloseWithoutOpenShouldBeNoOp () {
43
+ Assert .assertEquals (NONE , webSocketClient .getState ());
44
+ webSocketClient .close ();
45
+ webSocketClient .send ("test" );
46
+ Assert .assertEquals (NONE , webSocketClient .getState ());
47
+ }
48
+
49
+ }
You can’t perform that action at this time.
0 commit comments