@@ -18,19 +18,31 @@ def loop_times(times)
1818 end
1919 end
2020
21+ def rate_limit_error? ( task )
22+ result = task [ 'result' ]
23+ return false unless result . is_a? ( Hash )
24+
25+ description = result [ 'description' ]
26+ return false unless description . is_a? ( String )
27+
28+ description . downcase . include? ( 'rate limit' )
29+ end
30+
31+ def fetch_task_with_retry ( task_id , attempt )
32+ @client . get_task ( task_id )
33+ rescue StandardError => e
34+ raise e if attempt >= 10
35+
36+ sleep ( 1 )
37+ nil
38+ end
39+
2140 def wait_for_task ( task_id , timeout_seconds : 120 )
2241 sleep ( 2 ) # Initial delay
2342
2443 timeout_seconds . times do |i |
25- begin
26- task = @client . get_task ( task_id )
27- rescue StandardError => e
28- if i < 10
29- sleep ( 1 )
30- next
31- end
32- raise e
33- end
44+ task = fetch_task_with_retry ( task_id , i )
45+ next if task . nil?
3446
3547 expect ( task [ 'id' ] ) . to eq ( task_id )
3648
@@ -40,11 +52,9 @@ def wait_for_task(task_id, timeout_seconds: 120)
4052 when 'completed'
4153 return task
4254 when 'failed'
43- if task [ 'result' ] &.dig ( 'description' ) &.downcase &.include? ( 'rate limit' )
44- sleep ( 2 )
45- next
46- end
47- raise "Task failed with result: #{ task [ 'result' ] } "
55+ raise "Task failed with result: #{ task [ 'result' ] } " unless rate_limit_error? ( task )
56+
57+ sleep ( 2 )
4858 end
4959 end
5060
@@ -108,7 +118,7 @@ def wait_for_task(task_id, timeout_seconds: 120)
108118 {
109119 operation : 'addMembers' ,
110120 filter : { cid : { '$in' => [ @channel1 . cid , @channel2 . cid ] } } ,
111- members : [ @random_users [ 0 ] [ :id ] ]
121+ members : [ { 'user_id' => @random_users [ 0 ] [ :id ] } ]
112122 }
113123 )
114124
@@ -120,7 +130,8 @@ def wait_for_task(task_id, timeout_seconds: 120)
120130 it 'adds members to channels matching filter' do
121131 updater = @client . channel_batch_updater
122132
123- members = @random_users . map { |u | u [ :id ] }
133+ member_ids = @random_users . map { |u | u [ :id ] }
134+ members = member_ids . map { |id | { 'user_id' => id } }
124135 response = updater . add_members (
125136 { cid : { '$in' => [ @channel1 . cid , @channel2 . cid ] } } ,
126137 members
@@ -136,7 +147,7 @@ def wait_for_task(task_id, timeout_seconds: 120)
136147 ch1_state = @channel1 . query
137148 ch1_member_ids = ch1_state [ 'members' ] . map { |m | m [ 'user_id' ] }
138149
139- members . each do |member_id |
150+ member_ids . each do |member_id |
140151 expect ( ch1_member_ids ) . to include ( member_id )
141152 end
142153 end
@@ -174,7 +185,7 @@ def wait_for_task(task_id, timeout_seconds: 120)
174185
175186 response = updater . remove_members (
176187 { cid : { '$in' => [ @channel1 . cid , @channel2 . cid ] } } ,
177- [ member_to_remove ]
188+ [ { 'user_id' => member_to_remove } ]
178189 )
179190
180191 expect ( response [ 'task_id' ] ) . not_to be_empty
@@ -211,7 +222,7 @@ def wait_for_task(task_id, timeout_seconds: 120)
211222
212223 response = updater . archive (
213224 { cid : { '$in' => [ @channel1 . cid , @channel2 . cid ] } } ,
214- [ member_to_archive ]
225+ [ { 'user_id' => member_to_archive } ]
215226 )
216227
217228 expect ( response [ 'task_id' ] ) . not_to be_empty
0 commit comments