6
6
import rx .util .functions .Action1 ;
7
7
import rx .util .functions .Func0 ;
8
8
9
+ import static org .junit .Assert .assertFalse ;
9
10
import static org .junit .Assert .assertTrue ;
10
11
11
12
public class UnsubscribeTester
12
13
{
14
+ private boolean isDone = false ;
13
15
private Subscription subscription ;
14
16
15
17
public UnsubscribeTester () {}
@@ -59,17 +61,19 @@ private static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
59
61
@ Override
60
62
public void onCompleted ()
61
63
{
62
- test .doUnsubscribe ();
64
+ test .doUnsubscribe ("onCompleted" );
63
65
}
64
66
65
67
@ Override
66
68
public void onError (Exception e )
67
69
{
70
+ test .gotEvent ("onError" );
68
71
}
69
72
70
73
@ Override
71
74
public void onNext (T args )
72
75
{
76
+ test .gotEvent ("onNext" );
73
77
}
74
78
}));
75
79
return test ;
@@ -83,17 +87,19 @@ private static <T> UnsubscribeTester createOnError(Observable<T> observable)
83
87
@ Override
84
88
public void onCompleted ()
85
89
{
90
+ test .gotEvent ("onCompleted" );
86
91
}
87
92
88
93
@ Override
89
94
public void onError (Exception e )
90
95
{
91
- test .doUnsubscribe ();
96
+ test .doUnsubscribe ("onError" );
92
97
}
93
98
94
99
@ Override
95
100
public void onNext (T args )
96
101
{
102
+ test .gotEvent ("onNext" );
97
103
}
98
104
}));
99
105
return test ;
@@ -107,17 +113,19 @@ private static <T> UnsubscribeTester createOnNext(Observable<T> observable)
107
113
@ Override
108
114
public void onCompleted ()
109
115
{
116
+ test .gotEvent ("onCompleted" );
110
117
}
111
118
112
119
@ Override
113
120
public void onError (Exception e )
114
121
{
122
+ test .gotEvent ("onError" );
115
123
}
116
124
117
125
@ Override
118
126
public void onNext (T args )
119
127
{
120
- test .doUnsubscribe ();
128
+ test .doUnsubscribe ("onNext" );
121
129
}
122
130
}));
123
131
return test ;
@@ -128,15 +136,22 @@ private void setSubscription(Subscription subscription)
128
136
this .subscription = subscription ;
129
137
}
130
138
131
- private void doUnsubscribe ( )
139
+ private void gotEvent ( String event )
132
140
{
133
- Subscription subscription = this .subscription ;
134
- this .subscription = null ;
135
- subscription .unsubscribe ();
141
+ assertFalse ("received " + event + " after unsubscribe" , isDone );
142
+ }
143
+
144
+ private void doUnsubscribe (String event )
145
+ {
146
+ gotEvent (event );
147
+ if (subscription != null ) {
148
+ isDone = true ;
149
+ subscription .unsubscribe ();
150
+ }
136
151
}
137
152
138
153
private void assertPassed ()
139
154
{
140
- assertTrue ("expected notification was received" , subscription == null );
155
+ assertTrue ("expected notification was received" , isDone );
141
156
}
142
157
}
0 commit comments