66 "testing"
77 "time"
88
9+ "github.com/duneanalytics/cli/cmd/execution"
910 "github.com/duneanalytics/duneapi-client-go/models"
1011 "github.com/stretchr/testify/assert"
1112 "github.com/stretchr/testify/require"
@@ -67,7 +68,7 @@ func TestResultsJSONOutput(t *testing.T) {
6768 assert .Equal (t , "QUERY_STATE_COMPLETED" , got .State )
6869}
6970
70- func TestResultsPending (t * testing.T ) {
71+ func TestResultsPendingNoWait (t * testing.T ) {
7172 mock := & mockClient {
7273 queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
7374 return & models.ResultsResponse {
@@ -77,15 +78,15 @@ func TestResultsPending(t *testing.T) {
7778 }
7879
7980 root , buf := newTestRoot (mock )
80- root .SetArgs ([]string {"execution" , "results" , "01ABC" })
81+ root .SetArgs ([]string {"execution" , "results" , "--no-wait" , " 01ABC" })
8182 require .NoError (t , root .Execute ())
8283
8384 out := buf .String ()
8485 assert .Contains (t , out , "Execution ID: 01ABC" )
8586 assert .Contains (t , out , "State: QUERY_STATE_PENDING" )
8687}
8788
88- func TestResultsExecuting (t * testing.T ) {
89+ func TestResultsExecutingNoWait (t * testing.T ) {
8990 mock := & mockClient {
9091 queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
9192 return & models.ResultsResponse {
@@ -95,7 +96,7 @@ func TestResultsExecuting(t *testing.T) {
9596 }
9697
9798 root , buf := newTestRoot (mock )
98- root .SetArgs ([]string {"execution" , "results" , "01ABC" })
99+ root .SetArgs ([]string {"execution" , "results" , "--no-wait" , " 01ABC" })
99100 require .NoError (t , root .Execute ())
100101
101102 out := buf .String ()
@@ -179,3 +180,114 @@ func TestResultsMissingArgument(t *testing.T) {
179180 require .Error (t , err )
180181 assert .Contains (t , err .Error (), "accepts 1 arg(s)" )
181182}
183+
184+ func TestResultsWaitPollsUntilComplete (t * testing.T ) {
185+ execution .PollInterval = 0
186+ t .Cleanup (func () {
187+ execution .PollInterval = 2 * time .Second
188+ })
189+
190+ callCount := 0
191+ mock := & mockClient {
192+ queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
193+ callCount ++
194+ if callCount < 3 {
195+ return & models.ResultsResponse {
196+ State : "QUERY_STATE_EXECUTING" ,
197+ }, nil
198+ }
199+ return testResultsResponse , nil
200+ },
201+ }
202+
203+ root , buf := newTestRoot (mock )
204+ root .SetArgs ([]string {"execution" , "results" , "--timeout" , "10" , "01ABC" })
205+ require .NoError (t , root .Execute ())
206+
207+ assert .Equal (t , 3 , callCount )
208+ out := buf .String ()
209+ assert .Contains (t , out , "block_number" )
210+ assert .Contains (t , out , "2 rows" )
211+ }
212+
213+ func TestResultsWaitPollsUntilFailed (t * testing.T ) {
214+ execution .PollInterval = 0
215+ t .Cleanup (func () {
216+ execution .PollInterval = 2 * time .Second
217+ })
218+
219+ callCount := 0
220+ mock := & mockClient {
221+ queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
222+ callCount ++
223+ if callCount < 2 {
224+ return & models.ResultsResponse {
225+ State : "QUERY_STATE_PENDING" ,
226+ }, nil
227+ }
228+ return & models.ResultsResponse {
229+ State : "QUERY_STATE_FAILED" ,
230+ Error : & models.ExecutionError {
231+ Message : "out of memory" ,
232+ },
233+ }, nil
234+ },
235+ }
236+
237+ root , _ := newTestRoot (mock )
238+ root .SetArgs ([]string {"execution" , "results" , "--timeout" , "10" , "01ABC" })
239+ err := root .Execute ()
240+ require .Error (t , err )
241+ assert .Contains (t , err .Error (), "out of memory" )
242+ assert .Equal (t , 2 , callCount )
243+ }
244+
245+ func TestResultsWaitTimeout (t * testing.T ) {
246+ execution .PollInterval = 0
247+ t .Cleanup (func () {
248+ execution .PollInterval = 2 * time .Second
249+ })
250+
251+ callCount := 0
252+ mock := & mockClient {
253+ queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
254+ callCount ++
255+ return & models.ResultsResponse {
256+ State : "QUERY_STATE_EXECUTING" ,
257+ }, nil
258+ },
259+ }
260+
261+ root , _ := newTestRoot (mock )
262+ root .SetArgs ([]string {"execution" , "results" , "--timeout" , "1" , "01ABC" })
263+ err := root .Execute ()
264+ require .Error (t , err )
265+ assert .Contains (t , err .Error (), "timed out waiting for execution" )
266+
267+ }
268+
269+ func TestResultsWaitAPIError (t * testing.T ) {
270+ execution .PollInterval = 0
271+ t .Cleanup (func () {
272+ execution .PollInterval = 2 * time .Second
273+ })
274+
275+ callCount := 0
276+ mock := & mockClient {
277+ queryResultsV2Fn : func (_ string , _ models.ResultOptions ) (* models.ResultsResponse , error ) {
278+ callCount ++
279+ if callCount < 2 {
280+ return & models.ResultsResponse {
281+ State : "QUERY_STATE_PENDING" ,
282+ }, nil
283+ }
284+ return nil , errors .New ("api: rate limited" )
285+ },
286+ }
287+
288+ root , _ := newTestRoot (mock )
289+ root .SetArgs ([]string {"execution" , "results" , "--timeout" , "10" , "01ABC" })
290+ err := root .Execute ()
291+ require .Error (t , err )
292+ assert .Contains (t , err .Error (), "api: rate limited" )
293+ }
0 commit comments