@@ -42,3 +42,75 @@ func Test_Diag_GetWorkflowInstances(t *testing.T) {
4242 require .Len (t , instances , 1 )
4343 require .Equal (t , "ex1" , instances [0 ].Instance .InstanceID )
4444}
45+
46+ func Test_Diag_GetWorkflowInstances_Ordering (t * testing.T ) {
47+ if testing .Short () {
48+ t .Skip ()
49+ }
50+
51+ rclient := getClient ()
52+ setup := getCreateBackend (rclient )
53+
54+ b := setup ()
55+
56+ t .Cleanup (func () {
57+ require .NoError (t , b .Close ())
58+ })
59+
60+ bd := b .(diag.Backend )
61+ c := client .New (b )
62+ ctx := context .Background ()
63+
64+ for _ , id := range []string {"inst-a" , "inst-b" , "inst-c" } {
65+ _ , err := c .CreateWorkflowInstance (ctx , client.WorkflowInstanceOptions {InstanceID : id }, "some-workflow" )
66+ require .NoError (t , err )
67+ }
68+
69+ // Results should be newest-first (Rev: true).
70+ instances , err := bd .GetWorkflowInstances (ctx , "" , "" , 10 )
71+ require .NoError (t , err )
72+ require .Len (t , instances , 3 )
73+ require .Equal (t , "inst-c" , instances [0 ].Instance .InstanceID )
74+ require .Equal (t , "inst-a" , instances [2 ].Instance .InstanceID )
75+ }
76+
77+ func Test_Diag_GetWorkflowInstances_Pagination (t * testing.T ) {
78+ if testing .Short () {
79+ t .Skip ()
80+ }
81+
82+ rclient := getClient ()
83+ setup := getCreateBackend (rclient )
84+
85+ b := setup ()
86+
87+ t .Cleanup (func () {
88+ require .NoError (t , b .Close ())
89+ })
90+
91+ bd := b .(diag.Backend )
92+ c := client .New (b )
93+ ctx := context .Background ()
94+
95+ for _ , id := range []string {"inst-1" , "inst-2" , "inst-3" , "inst-4" } {
96+ _ , err := c .CreateWorkflowInstance (ctx , client.WorkflowInstanceOptions {InstanceID : id }, "some-workflow" )
97+ require .NoError (t , err )
98+ }
99+
100+ // First page: newest 2.
101+ page1 , err := bd .GetWorkflowInstances (ctx , "" , "" , 2 )
102+ require .NoError (t , err )
103+ require .Len (t , page1 , 2 )
104+
105+ // Second page: continue after last item of page 1.
106+ last := page1 [len (page1 )- 1 ]
107+ page2 , err := bd .GetWorkflowInstances (ctx , last .Instance .InstanceID , last .Instance .ExecutionID , 2 )
108+ require .NoError (t , err )
109+ require .Len (t , page2 , 2 )
110+
111+ // No overlap between pages.
112+ page1IDs := map [string ]bool {page1 [0 ].Instance .InstanceID : true , page1 [1 ].Instance .InstanceID : true }
113+ for _ , inst := range page2 {
114+ require .False (t , page1IDs [inst .Instance .InstanceID ], "duplicate instance across pages: %s" , inst .Instance .InstanceID )
115+ }
116+ }
0 commit comments