32
32
import org .junit .Test ;
33
33
34
34
public class ServiceWorkflowHistoryIteratorTest {
35
-
36
- public static final ByteString EMPTY_PAGE_TOKEN =
37
- ByteString .copyFrom ("empty page token" , Charset .defaultCharset ());
38
35
public static final ByteString NEXT_PAGE_TOKEN =
39
36
ByteString .copyFrom ("next token" , Charset .defaultCharset ());
37
+ public static final ByteString EMPTY_HISTORY_PAGE =
38
+ ByteString .copyFrom ("empty history page token" , Charset .defaultCharset ());
39
+ public static final ByteString NEXT_NEXT_PAGE_TOKEN =
40
+ ByteString .copyFrom ("next next token" , Charset .defaultCharset ());
41
+ public static final ByteString EMPTY_PAGE_TOKEN =
42
+ ByteString .copyFrom ("empty page token" , Charset .defaultCharset ());
40
43
41
44
/*
42
45
This test Scenario verifies following things:
43
46
1. hasNext() method makes a call to the server to retrieve workflow history when current
44
47
history is empty and history token is available and cached the result.
45
48
2. next() method reuses cached history when possible.
46
- 3. hasNext() fetches an empty page and return false.
47
- 4. next() throws NoSuchElementException when neither history no history token is available.
49
+ 3. hasNext() keeps fetching as long as the server returns a next page token.
50
+ 4. hasNext() fetches an empty page and return false.
51
+ 5. next() throws NoSuchElementException when neither history no history token is available.
48
52
*/
49
53
@ Test
50
54
public void verifyHasNextIsFalseWhenHistoryIsEmpty () {
@@ -58,13 +62,22 @@ public void verifyHasNextIsFalseWhenHistoryIsEmpty() {
58
62
GetWorkflowExecutionHistoryResponse queryWorkflowExecutionHistory () {
59
63
timesCalledServer .incrementAndGet ();
60
64
try {
65
+ History history = HistoryUtils .generateWorkflowTaskWithInitialHistory ().getHistory ();
61
66
if (EMPTY_PAGE_TOKEN .equals (nextPageToken )) {
62
67
return GetWorkflowExecutionHistoryResponse .newBuilder ().build ();
68
+ } else if (EMPTY_HISTORY_PAGE .equals (nextPageToken )) {
69
+ return GetWorkflowExecutionHistoryResponse .newBuilder ()
70
+ .setNextPageToken (NEXT_NEXT_PAGE_TOKEN )
71
+ .build ();
72
+ } else if (NEXT_NEXT_PAGE_TOKEN .equals (nextPageToken )) {
73
+ return GetWorkflowExecutionHistoryResponse .newBuilder ()
74
+ .setHistory (history )
75
+ .setNextPageToken (EMPTY_PAGE_TOKEN )
76
+ .build ();
63
77
}
64
- History history = HistoryUtils .generateWorkflowTaskWithInitialHistory ().getHistory ();
65
78
return GetWorkflowExecutionHistoryResponse .newBuilder ()
66
79
.setHistory (history )
67
- .setNextPageToken (EMPTY_PAGE_TOKEN )
80
+ .setNextPageToken (EMPTY_HISTORY_PAGE )
68
81
.build ();
69
82
} catch (Exception e ) {
70
83
throw new RuntimeException (e );
@@ -80,9 +93,15 @@ GetWorkflowExecutionHistoryResponse queryWorkflowExecutionHistory() {
80
93
Assert .assertTrue (iterator .hasNext ());
81
94
Assert .assertNotNull (iterator .next ());
82
95
Assert .assertEquals (1 , timesCalledServer .get ());
96
+ Assert .assertTrue (iterator .hasNext ());
97
+ Assert .assertEquals (3 , timesCalledServer .get ());
98
+ Assert .assertNotNull (iterator .next ());
99
+ Assert .assertTrue (iterator .hasNext ());
100
+ Assert .assertNotNull (iterator .next ());
101
+ Assert .assertTrue (iterator .hasNext ());
102
+ Assert .assertNotNull (iterator .next ());
83
103
Assert .assertFalse (iterator .hasNext ());
84
- Assert .assertEquals (2 , timesCalledServer .get ());
85
104
Assert .assertThrows (NoSuchElementException .class , iterator ::next );
86
- Assert .assertEquals (2 , timesCalledServer .get ());
105
+ Assert .assertEquals (4 , timesCalledServer .get ());
87
106
}
88
107
}
0 commit comments