18
18
19
19
import static org .junit .jupiter .api .Assertions .*;
20
20
21
+ import java .util .ArrayList ;
21
22
import java .util .List ;
23
+ import java .util .Locale ;
22
24
import java .util .concurrent .Callable ;
23
25
import java .util .concurrent .ExecutorService ;
24
26
import java .util .concurrent .Executors ;
@@ -68,21 +70,32 @@ void beforeEach() {
68
70
69
71
@ Test
70
72
void shouldSaveAndFindAll () {
71
- Airport vie = new Airport ("airports::vie" , "vie" , "loww" );
72
- airportRepository .save (vie );
73
-
74
- List <Airport > all = StreamSupport .stream (airportRepository .findAll ().spliterator (), false )
75
- .collect (Collectors .toList ());
76
-
77
- assertFalse (all .isEmpty ());
78
- assertTrue (all .stream ().anyMatch (a -> a .getId ().equals ("airports::vie" )));
73
+ Airport vie = null ;
74
+ try {
75
+ vie = new Airport ("airports::vie" , "vie" , "loww" );
76
+ airportRepository .save (vie );
77
+ List <Airport > all = new ArrayList <>();
78
+ airportRepository .findAll ().forEach (all ::add );
79
+ assertFalse (all .isEmpty ());
80
+ assertTrue (all .stream ().anyMatch (a -> a .getId ().equals ("airports::vie" )));
81
+ } finally {
82
+ airportRepository .delete (vie );
83
+ }
79
84
}
80
85
81
86
@ Test
82
87
void findBySimpleProperty () {
83
- List <Airport > airports = airportRepository .findAllByIata ("vie" );
84
- // TODO
85
- System .err .println (airports );
88
+ Airport vie = null ;
89
+ try {
90
+ vie = new Airport ("airports::vie" , "vie" , "loww" );
91
+ airportRepository .save (vie );
92
+ sleep (1000 );
93
+ List <Airport > airports = airportRepository .findAllByIata ("vie" );
94
+ assertEquals (vie .getId (), airports .get (0 ).getId ());
95
+ } finally {
96
+ airportRepository .delete (vie );
97
+ }
98
+
86
99
}
87
100
88
101
@ Test
@@ -94,12 +107,11 @@ void count() {
94
107
try {
95
108
Callable <Boolean >[] suppliers = new Callable [iatas .length ];
96
109
for (int i = 0 ; i < iatas .length ; i ++) {
97
- Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ , iatas [i ].toLowerCase () /* lcao */ );
110
+ Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ ,
111
+ iatas [i ].toLowerCase (Locale .ROOT ) /* lcao */ );
98
112
airportRepository .save (airport );
99
113
}
100
- try {
101
- Thread .sleep (1000 );
102
- } catch (InterruptedException ie ) {}
114
+ sleep (1000 );
103
115
long airportCount = 0 ;
104
116
airportCount = airportRepository .count ();
105
117
assertEquals (7 , airportCount );
@@ -110,6 +122,9 @@ void count() {
110
122
airportCount = airportRepository .countByIcaoAndIataIn ("jfk" , "JFK" , "IAD" , "SFO" , "XXX" );
111
123
assertEquals (1 , airportCount );
112
124
125
+ airportCount = airportRepository .countByIcaoOrIataIn ("jfk" , "LAX" , "IAD" , "SFO" );
126
+ assertEquals (4 , airportCount );
127
+
113
128
airportCount = airportRepository .countByIataIn ("XXX" );
114
129
assertEquals (0 , airportCount );
115
130
@@ -128,29 +143,30 @@ void threadSafeParametersTest() throws Exception {
128
143
ExecutorService executorService = Executors .newFixedThreadPool (iatas .length );
129
144
130
145
try {
131
- Callable <Boolean >[] suppliers = new Callable [iatas .length ];
132
146
for (int i = 0 ; i < iatas .length ; i ++) {
133
- Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ , iatas [i ].toLowerCase () /* lcao */ );
147
+ Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ ,
148
+ iatas [i ].toLowerCase (Locale .ROOT ) /* lcao */ );
134
149
airportRepository .save (airport );
135
- final int idx = i ;
136
- suppliers [i ] = () -> {
137
- System .out .println (Thread .currentThread () + " " + iatas [idx ] + " ->" );
138
- try {
139
- Thread .sleep (iatas .length - idx ); // so they are executed out-of-order
140
- } catch (InterruptedException ie ) {
141
- ;
142
- }
143
- String foundName = airportRepository .findAllByIata (iatas [idx ]).get (0 ).getIata ();
144
- System .out .println (Thread .currentThread () + " " + iatas [idx ] + " <- " );
145
- assertEquals (iatas [idx ], foundName );
146
- return iatas [idx ].equals (foundName );
147
- };
148
150
}
149
- for (int i = 0 ; i < iatas .length ; i ++) {
150
- future [i ] = executorService .submit (suppliers [i ]);
151
- }
152
- for (int i = 0 ; i < iatas .length ; i ++) {
153
- future [i ].get ();
151
+ sleep (1000 );
152
+ for (int k = 0 ; k < 50 ; k ++) {
153
+ Callable <Boolean >[] suppliers = new Callable [iatas .length ];
154
+ for (int i = 0 ; i < iatas .length ; i ++) {
155
+ final int idx = i ;
156
+ suppliers [i ] = () -> {
157
+ sleep (iatas .length - idx ); // so they are executed out-of-order
158
+ List <Airport > airports = airportRepository .findAllByIata (iatas [idx ]);
159
+ String foundName = airportRepository .findAllByIata (iatas [idx ]).get (0 ).getIata ();
160
+ assertEquals (iatas [idx ], foundName );
161
+ return iatas [idx ].equals (foundName );
162
+ };
163
+ }
164
+ for (int i = 0 ; i < iatas .length ; i ++) {
165
+ future [i ] = executorService .submit (suppliers [i ]);
166
+ }
167
+ for (int i = 0 ; i < iatas .length ; i ++) {
168
+ future [i ].get (); // check is done in Callable
169
+ }
154
170
}
155
171
156
172
} finally {
@@ -163,35 +179,34 @@ void threadSafeParametersTest() throws Exception {
163
179
}
164
180
165
181
@ Test
166
- void threadSafeStringParametersTest () throws Exception {
182
+ void threadSafeStringParametersTest () throws Exception {
167
183
String [] iatas = { "JFK" , "IAD" , "SFO" , "SJC" , "SEA" , "LAX" , "PHX" };
168
184
Future [] future = new Future [iatas .length ];
169
185
ExecutorService executorService = Executors .newFixedThreadPool (iatas .length );
170
186
171
187
try {
172
- Callable <Boolean >[] suppliers = new Callable [iatas .length ];
173
188
for (int i = 0 ; i < iatas .length ; i ++) {
174
- Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ , iatas [i ] /* lcao */ );
189
+ Airport airport = new Airport ("airports::" + iatas [i ], iatas [i ] /*iata*/ , iatas [i ]. toLowerCase () /* lcao */ );
175
190
airportRepository .save (airport );
176
- final int idx = i ;
177
- suppliers [i ] = () -> {
178
- System .out .println (Thread .currentThread () + " " + iatas [idx ] + " ->" );
179
- try {
180
- Thread .sleep (iatas .length - idx ); // so they are executed out-of-order
181
- } catch (InterruptedException ie ) {
182
- ;
183
- }
184
- String foundName = airportRepository .getAllByIata (iatas [idx ]).get (0 ).getIata ();
185
- System .out .println (Thread .currentThread () + " " + iatas [idx ] + " <- " );
186
- assertEquals (iatas [idx ], foundName );
187
- return iatas [idx ].equals (foundName );
188
- };
189
191
}
190
- for (int i = 0 ; i < iatas .length ; i ++) {
191
- future [i ] = executorService .submit (suppliers [i ]);
192
- }
193
- for (int i = 0 ; i < iatas .length ; i ++) {
194
- future [i ].get ();
192
+ sleep (1000 );
193
+ for (int k = 0 ; k < 100 ; k ++) {
194
+ Callable <Boolean >[] suppliers = new Callable [iatas .length ];
195
+ for (int i = 0 ; i < iatas .length ; i ++) {
196
+ final int idx = i ;
197
+ suppliers [i ] = () -> {
198
+ sleep (iatas .length - idx ); // so they are executed out-of-order
199
+ String foundName = airportRepository .getAllByIata (iatas [idx ]).get (0 ).getIata ();
200
+ assertEquals (iatas [idx ], foundName );
201
+ return iatas [idx ].equals (foundName );
202
+ };
203
+ }
204
+ for (int i = 0 ; i < iatas .length ; i ++) {
205
+ future [i ] = executorService .submit (suppliers [i ]);
206
+ }
207
+ for (int i = 0 ; i < iatas .length ; i ++) {
208
+ future [i ].get (); // check is done in Callable
209
+ }
195
210
}
196
211
} finally {
197
212
executorService .shutdown ();
@@ -202,6 +217,14 @@ void threadSafeStringParametersTest() throws Exception {
202
217
}
203
218
}
204
219
220
+ private void sleep (int millis ) {
221
+ try {
222
+ Thread .sleep (millis ); // so they are executed out-of-order
223
+ } catch (InterruptedException ie ) {
224
+ ;
225
+ }
226
+ }
227
+
205
228
@ Configuration
206
229
@ EnableCouchbaseRepositories ("org.springframework.data.couchbase" )
207
230
static class Config extends AbstractCouchbaseConfiguration {
0 commit comments