16
16
17
17
package com .google .cloud .spanner .connection ;
18
18
19
- import static org .hamcrest .CoreMatchers .equalTo ;
20
- import static org .hamcrest .CoreMatchers .is ;
21
- import static org .hamcrest .MatcherAssert .assertThat ;
22
19
import static org .junit .Assert .assertEquals ;
23
20
import static org .junit .Assert .assertNotNull ;
24
21
import static org .junit .Assert .assertThrows ;
59
56
import java .util .concurrent .TimeoutException ;
60
57
import org .junit .After ;
61
58
import org .junit .Before ;
59
+ import org .junit .Rule ;
62
60
import org .junit .Test ;
61
+ import org .junit .rules .Timeout ;
63
62
import org .junit .runner .RunWith ;
64
63
import org .junit .runners .Parameterized ;
65
64
import org .junit .runners .Parameterized .Parameter ;
@@ -90,12 +89,18 @@ public class StatementTimeoutTest extends AbstractMockServerTest {
90
89
*/
91
90
private static final int TIMEOUT_FOR_SLOW_STATEMENTS = 50 ;
92
91
92
+ // Set a global timeout to ensure that tests that freeze the mock serer fail within a reasonable
93
+ // amount of time if they misbehave.
94
+ @ Rule public Timeout globalTimeout = Timeout .seconds (10 );
95
+
93
96
@ Parameters (name = "statementExecutorType = {0}" )
94
97
public static Object [] parameters () {
95
98
return StatementExecutorType .values ();
96
99
}
97
100
98
- @ Parameter public StatementExecutorType statementExecutorType ;
101
+ @ SuppressWarnings ("ClassEscapesDefinedScope" )
102
+ @ Parameter
103
+ public StatementExecutorType statementExecutorType ;
99
104
100
105
protected ITConnection createConnection () {
101
106
ConnectionOptions options =
@@ -423,7 +428,7 @@ public void testTimeoutExceptionReadWriteTransactionalSlowCommit() {
423
428
}
424
429
425
430
connection .setStatementTimeout (TIMEOUT_FOR_SLOW_STATEMENTS , TimeUnit .MILLISECONDS );
426
- SpannerException e = assertThrows (SpannerException .class , () -> connection . commit () );
431
+ SpannerException e = assertThrows (SpannerException .class , connection :: commit );
427
432
assertEquals (ErrorCode .DEADLINE_EXCEEDED , e .getErrorCode ());
428
433
}
429
434
}
@@ -613,10 +618,10 @@ private void waitForDdlRequestOnServer() {
613
618
try {
614
619
Stopwatch watch = Stopwatch .createStarted ();
615
620
while (Collections2 .filter (
616
- mockDatabaseAdmin .getRequests (),
617
- input -> input .getClass ().equals (UpdateDatabaseDdlRequest .class ))
618
- . size ()
619
- == 0 ) {
621
+ mockDatabaseAdmin .getRequests (),
622
+ input -> input .getClass ().equals (UpdateDatabaseDdlRequest .class ))
623
+ . isEmpty ()) {
624
+ //noinspection BusyWait
620
625
Thread .sleep (1L );
621
626
if (watch .elapsed (TimeUnit .MILLISECONDS ) > EXECUTION_TIME_SLOW_STATEMENT ) {
622
627
throw new TimeoutException ("Timeout while waiting for DDL request" );
@@ -680,10 +685,10 @@ public void testCancelReadOnlyAutocommitMultipleStatements() {
680
685
connection .cancel ();
681
686
});
682
687
683
- SpannerException e =
688
+ SpannerException exception =
684
689
assertThrows (
685
690
SpannerException .class , () -> connection .executeQuery (SELECT_RANDOM_STATEMENT ));
686
- assertThat ( e . getErrorCode (), is ( equalTo ( ErrorCode . CANCELLED ) ));
691
+ assertEquals ( ErrorCode . CANCELLED , exception . getErrorCode ( ));
687
692
688
693
mockSpanner .removeAllExecutionTimes ();
689
694
connection .setStatementTimeout (TIMEOUT_FOR_FAST_STATEMENTS , TimeUnit .MILLISECONDS );
@@ -1000,7 +1005,7 @@ public void testCancelDdlBatch() {
1000
1005
waitForDdlRequestOnServer ();
1001
1006
connection .cancel ();
1002
1007
});
1003
- SpannerException e = assertThrows (SpannerException .class , () -> connection . runBatch () );
1008
+ SpannerException e = assertThrows (SpannerException .class , connection :: runBatch );
1004
1009
assertEquals (ErrorCode .CANCELLED , e .getErrorCode ());
1005
1010
} finally {
1006
1011
executor .shutdownNow ();
@@ -1078,10 +1083,10 @@ public void testTimeoutExceptionDdlBatch() {
1078
1083
connection .startBatchDdl ();
1079
1084
connection .setStatementTimeout (TIMEOUT_FOR_SLOW_STATEMENTS , TimeUnit .MILLISECONDS );
1080
1085
1081
- // the following statement will NOT timeout as the statement is only buffered locally
1086
+ // the following statement will NOT time out as the statement is only buffered locally
1082
1087
connection .execute (Statement .of (SLOW_DDL ));
1083
- // the runBatch() statement sends the statement to the server and should timeout
1084
- SpannerException e = assertThrows (SpannerException .class , () -> connection . runBatch () );
1088
+ // the runBatch() statement sends the statement to the server and should time out
1089
+ SpannerException e = assertThrows (SpannerException .class , connection :: runBatch );
1085
1090
assertEquals (ErrorCode .DEADLINE_EXCEEDED , e .getErrorCode ());
1086
1091
}
1087
1092
}
@@ -1098,7 +1103,7 @@ public void testTimeoutExceptionDdlBatchMultipleStatements() {
1098
1103
for (int i = 0 ; i < 2 ; i ++) {
1099
1104
connection .startBatchDdl ();
1100
1105
connection .execute (Statement .of (SLOW_DDL ));
1101
- SpannerException e = assertThrows (SpannerException .class , () -> connection . runBatch () );
1106
+ SpannerException e = assertThrows (SpannerException .class , connection :: runBatch );
1102
1107
assertEquals (ErrorCode .DEADLINE_EXCEEDED , e .getErrorCode ());
1103
1108
}
1104
1109
// try to do a new DDL statement that is fast.
0 commit comments