mturk 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +2 -0
  4. data/.gemtest +0 -0
  5. data/History.md +105 -0
  6. data/LICENSE.txt +202 -0
  7. data/Manifest.txt +72 -0
  8. data/NOTICE.txt +4 -0
  9. data/README.md +100 -0
  10. data/Rakefile +33 -0
  11. data/bin/mturk +9 -0
  12. data/lib/amazon/util.rb +10 -0
  13. data/lib/amazon/util/binder.rb +48 -0
  14. data/lib/amazon/util/data_reader.rb +169 -0
  15. data/lib/amazon/util/filter_chain.rb +79 -0
  16. data/lib/amazon/util/hash_nesting.rb +93 -0
  17. data/lib/amazon/util/lazy_results.rb +59 -0
  18. data/lib/amazon/util/logging.rb +23 -0
  19. data/lib/amazon/util/paginated_iterator.rb +70 -0
  20. data/lib/amazon/util/proactive_results.rb +116 -0
  21. data/lib/amazon/util/threadpool.rb +129 -0
  22. data/lib/amazon/util/user_data_store.rb +100 -0
  23. data/lib/amazon/webservices/mechanical_turk.rb +123 -0
  24. data/lib/amazon/webservices/mechanical_turk_requester.rb +285 -0
  25. data/lib/amazon/webservices/mturk/mechanical_turk_error_handler.rb +153 -0
  26. data/lib/amazon/webservices/mturk/question_generator.rb +58 -0
  27. data/lib/amazon/webservices/util/amazon_authentication_relay.rb +72 -0
  28. data/lib/amazon/webservices/util/command_line.rb +155 -0
  29. data/lib/amazon/webservices/util/convenience_wrapper.rb +90 -0
  30. data/lib/amazon/webservices/util/filter_proxy.rb +45 -0
  31. data/lib/amazon/webservices/util/mock_transport.rb +70 -0
  32. data/lib/amazon/webservices/util/request_signer.rb +42 -0
  33. data/lib/amazon/webservices/util/rest_transport.rb +120 -0
  34. data/lib/amazon/webservices/util/soap_simplifier.rb +48 -0
  35. data/lib/amazon/webservices/util/soap_transport.rb +20 -0
  36. data/lib/amazon/webservices/util/soap_transport_header_handler.rb +27 -0
  37. data/lib/amazon/webservices/util/unknown_result_exception.rb +27 -0
  38. data/lib/amazon/webservices/util/validation_exception.rb +55 -0
  39. data/lib/amazon/webservices/util/xml_simplifier.rb +61 -0
  40. data/lib/mturk.rb +19 -0
  41. data/lib/mturk/version.rb +6 -0
  42. data/run_rcov.sh +1 -0
  43. data/samples/best_image/BestImage.rb +61 -0
  44. data/samples/best_image/best_image.properties +39 -0
  45. data/samples/best_image/best_image.question +82 -0
  46. data/samples/blank_slate/BlankSlate.rb +63 -0
  47. data/samples/blank_slate/BlankSlate_multithreaded.rb +67 -0
  48. data/samples/helloworld/MTurkHelloWorld.rb +56 -0
  49. data/samples/helloworld/mturk.yml +8 -0
  50. data/samples/review_policy/ReviewPolicy.rb +139 -0
  51. data/samples/review_policy/review_policy.question +30 -0
  52. data/samples/reviewer/Reviewer.rb +103 -0
  53. data/samples/reviewer/mturk.yml +8 -0
  54. data/samples/simple_survey/SimpleSurvey.rb +98 -0
  55. data/samples/simple_survey/simple_survey.question +30 -0
  56. data/samples/site_category/SiteCategory.rb +87 -0
  57. data/samples/site_category/externalpage.htm +71 -0
  58. data/samples/site_category/site_category.input +6 -0
  59. data/samples/site_category/site_category.properties +56 -0
  60. data/samples/site_category/site_category.question +9 -0
  61. data/test/mturk/test_changehittypeofhit.rb +130 -0
  62. data/test/mturk/test_error_handler.rb +403 -0
  63. data/test/mturk/test_mechanical_turk_requester.rb +178 -0
  64. data/test/mturk/test_mock_mechanical_turk_requester.rb +205 -0
  65. data/test/test_mturk.rb +21 -0
  66. data/test/unit/test_binder.rb +89 -0
  67. data/test/unit/test_data_reader.rb +135 -0
  68. data/test/unit/test_exceptions.rb +32 -0
  69. data/test/unit/test_hash_nesting.rb +99 -0
  70. data/test/unit/test_lazy_results.rb +89 -0
  71. data/test/unit/test_mock_transport.rb +132 -0
  72. data/test/unit/test_paginated_iterator.rb +58 -0
  73. data/test/unit/test_proactive_results.rb +108 -0
  74. data/test/unit/test_question_generator.rb +55 -0
  75. data/test/unit/test_threadpool.rb +50 -0
  76. data/test/unit/test_user_data_store.rb +80 -0
  77. metadata +225 -0
  78. metadata.gz.sig +0 -0
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0"?>
2
+ <!-- Note the inclusion of the @urls variable which is defined as a field in the input file. ERB is the template engine that is
3
+ used to perform the merging of variables into template files.
4
+ -->
5
+ <% require 'cgi' %>
6
+ <ExternalQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd">
7
+ <ExternalURL>https://s3.amazonaws.com/mturk/samples/sitecategory/externalpage.htm?url=<%= CGI::escape @urls %></ExternalURL>
8
+ <FrameHeight>400</FrameHeight>
9
+ </ExternalQuestion>
@@ -0,0 +1,130 @@
1
+ # Copyright:: Copyright (c) 2007 Amazon Technologies, Inc.
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'test/unit/testcase'
5
+ require 'mturk'
6
+ require 'amazon/webservices/mturk/question_generator'
7
+ require 'amazon/webservices/util/soap_transport'
8
+ require 'amazon/webservices/util/rest_transport'
9
+ require 'amazon/webservices/util/mock_transport'
10
+
11
+
12
+ class TestChangeHITTypeOfHIT < Test::Unit::TestCase
13
+ include Amazon::WebServices
14
+
15
+ def setup
16
+ # Setting up the default requester interface.
17
+ # This will default to running against Sandbox, but we specify the host explicitly for safety.
18
+ # Access Key ID and Secret Access Key will be loaded from user preference file
19
+ # If not found, the user will be interatively queried for their authentication information.
20
+ @@mturk ||= Amazon::WebServices::MechanicalTurkRequester.new :Host => :Sandbox if ENV['DISABLE_MTURK_SANDBOX_TEST'].nil?
21
+
22
+ # set up a mock version of the service
23
+ @@mock ||= Amazon::WebServices::Util::MockTransport.new
24
+ @@mockturk ||= Amazon::WebServices::MechanicalTurkRequester.new( :Transport => @@mock, :AWSAccessKey => 'bogus', :AWSAccessKeyId => 'fake' )
25
+
26
+ # lets just build this question once
27
+ @@question ||= MTurk::QuestionGenerator.build { |q| q.ask 'Have you tried changing the description of a HIT?' }
28
+ end
29
+
30
+ def testUpdateHITMock
31
+ @@mock.listen do |call|
32
+ case call.name
33
+ when :GetHIT
34
+ {:HIT => {:HITId => call.request[:HITId], :Title => "old title", :Description => "foo", :Request => {} } }
35
+ when :RegisterHITType
36
+ {:RegisterHITTypeResult => {:HITTypeId => 'mockHITType', :Request => {} } }
37
+ else
38
+ {}
39
+ end
40
+ end
41
+
42
+ @@mockturk.updateHIT( 'mockHIT', { :Title => 'new title' } )
43
+ assert_equal :GetHIT, @@mock.next.name
44
+
45
+ ht = @@mock.next
46
+ assert_equal :RegisterHITType, ht.name
47
+ assert_equal "new title", ht.request[:Title]
48
+ assert_equal "foo", ht.request[:Description]
49
+
50
+ update = @@mock.next
51
+ assert_equal :ChangeHITTypeOfHIT, update.name
52
+ assert_equal 'mockHIT', update.request[:HITId]
53
+ assert_equal 'mockHITType', update.request[:HITTypeId]
54
+ end
55
+
56
+ # Allow user to skip these tests by setting the DISABLE_MTURK_SANDBOX_TEST environment variable
57
+ if ENV['DISABLE_MTURK_SANDBOX_TEST'].nil?
58
+
59
+ def testHappyCase
60
+ hit_result = @@mturk.createHIT :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT)',
61
+ :Description => 'Autogenerated mturk test case description1',
62
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description1',
63
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' },
64
+ :Question => @@question
65
+ new_hit_type = @@mturk.registerHITType :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT) - Updated',
66
+ :Description => 'Autogenerated mturk test case description2',
67
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description2',
68
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' }
69
+ @@mturk.changeHITTypeOfHIT :HITId => hit_result[:HITId],
70
+ :HITTypeId => new_hit_type[:HITTypeId]
71
+ updated_hit = @@mturk.getHIT :HITId => hit_result[:HITId]
72
+ assert_equal new_hit_type[:HITTypeId], updated_hit[:HITTypeId], 'HITTypeId of ' + hit_result[:HITId] + ' not updated'
73
+ @@mturk.forceExpireHIT :HITId => hit_result[:HITId]
74
+ @@mturk.disposeHIT :HITId => hit_result[:HITId]
75
+ end
76
+
77
+ def testUpdateHIT
78
+ hit_result = @@mturk.createHIT :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT)',
79
+ :Description => 'Autogenerated mturk test case description1',
80
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description1',
81
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' },
82
+ :Question => @@question
83
+ new_template = { :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT) - Updated',
84
+ :Description => 'Autogenerated mturk test case description2',
85
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description2',
86
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' } }
87
+ @@mturk.updateHIT(hit_result[:HITId], new_template)
88
+ assert_not_equal hit_result[:HITTypeId], @@mturk.getHIT(:HITId=>hit_result[:HITId])[:HITTypeId], 'HITTypeId not changed'
89
+ @@mturk.forceExpireHIT :HITId => hit_result[:HITId]
90
+ @@mturk.disposeHIT :HITId => hit_result[:HITId]
91
+ end
92
+
93
+ def testUpdateHITs
94
+ hit_result1 = @@mturk.createHIT :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT)',
95
+ :Description => 'Autogenerated mturk test case description1',
96
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description1',
97
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' },
98
+ :Question => @@question
99
+ hit_result2 = @@mturk.createHIT :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT)',
100
+ :Description => 'Autogenerated mturk test case description1',
101
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description1',
102
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' },
103
+ :Question => @@question
104
+ hit_result3 = @@mturk.createHIT :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT)',
105
+ :Description => 'Autogenerated mturk test case description1',
106
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description1',
107
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' },
108
+ :Question => @@question
109
+ new_template = { :Title => 'Ruby SDK Test Hit (ChangeHITTypeOfHIT) - Updated',
110
+ :Description => 'Autogenerated mturk test case description2',
111
+ :Keywords => 'Ruby, test, SDK, changehittypeofhit, description2',
112
+ :Reward => { :Amount => 0.01, :CurrencyCode => 'USD' } }
113
+
114
+ @@mturk.updateHITs(new_template, [hit_result1[:HITId], hit_result2[:HITId], hit_result3[:HITId]])
115
+
116
+ assert_not_equal hit_result1[:HITTypeId], @@mturk.getHIT(:HITId=>hit_result1[:HITId])[:HITTypeId], 'HITTypeId not changed'
117
+ assert_not_equal hit_result2[:HITTypeId], @@mturk.getHIT(:HITId=>hit_result2[:HITId])[:HITTypeId], 'HITTypeId not changed'
118
+ assert_not_equal hit_result3[:HITTypeId], @@mturk.getHIT(:HITId=>hit_result3[:HITId])[:HITTypeId], 'HITTypeId not changed'
119
+ @@mturk.forceExpireHIT :HITId => hit_result1[:HITId]
120
+ @@mturk.disposeHIT :HITId => hit_result1[:HITId]
121
+ @@mturk.forceExpireHIT :HITId => hit_result2[:HITId]
122
+ @@mturk.disposeHIT :HITId => hit_result2[:HITId]
123
+ @@mturk.forceExpireHIT :HITId => hit_result3[:HITId]
124
+ @@mturk.disposeHIT :HITId => hit_result3[:HITId]
125
+ end
126
+
127
+ end # if not DISABLE_MTURK_SANDBOX_TEST
128
+
129
+ end
130
+
@@ -0,0 +1,403 @@
1
+ # Copyright:: Copyright (c) 2007-2014 Amazon Technologies, Inc.
2
+ # License:: Apache License, Version 2.0
3
+
4
+ require 'test/unit/testcase'
5
+ require 'mturk'
6
+ require 'amazon/webservices/util/mock_transport'
7
+ require 'timeout'
8
+ require 'amazon/webservices/util/validation_exception'
9
+
10
+ class TestErrorHandler < Test::Unit::TestCase
11
+ RETRYABLE_ATTEMPT_COUNT = 7
12
+
13
+ def setup
14
+ @mock = Amazon::WebServices::Util::MockTransport.new
15
+ @mturk = Amazon::WebServices::MechanicalTurkRequester.new( :Transport => @mock, :AWSAccessKey => 'bogus', :AWSAccessKeyId => 'fake' )
16
+ end
17
+
18
+ def testTimeoutOnceRetryable
19
+ # mock will timeout first time, return success on second call
20
+ should_fail = true
21
+ @mock.listen do |call|
22
+ Timeout.timeout(1) do
23
+ if should_fail
24
+ should_fail = false
25
+ sleep(2)
26
+ end
27
+ end
28
+ nil
29
+ end
30
+
31
+ # invoke a retryable call once, should auto-retry and return success
32
+ @mturk.getAccountBalance
33
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
34
+ @mock.each {|call| assert_equal :GetAccountBalance, call.name, "Should have been a GetAccountBalance call" }
35
+ end
36
+
37
+ def testTimeoutOnceNotRetryable
38
+ # mock will timeout first time, return success on second call
39
+ should_fail = true
40
+ @mock.listen do |call|
41
+ Timeout.timeout(1) do
42
+ if should_fail
43
+ should_fail = false
44
+ sleep(2)
45
+ end
46
+ end
47
+ nil
48
+ end
49
+
50
+ # invoking a non-retryable call will throw an exception
51
+ begin
52
+ @mturk.grantBonus
53
+ fail "Should have thrown an exception"
54
+ rescue Timeout::Error
55
+ # expect this exception
56
+ end
57
+ assert_equal :GrantBonus, @mock.next.name
58
+ assert_nil @mock.next
59
+ end
60
+
61
+ def testTimeoutOnceRetryableUniqueRequestToken
62
+ # mock will timeout first time, return success on second call
63
+ should_fail = true
64
+ @mock.listen do |call|
65
+ Timeout.timeout(1) do
66
+ if should_fail
67
+ should_fail = false
68
+ sleep(2)
69
+ end
70
+ end
71
+ nil
72
+ end
73
+
74
+ # invoke call with an idempotency once, should auto-retry and return success
75
+ @mturk.grantBonus :UniqueRequestToken => 'UniqueTokenValue'
76
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
77
+ @mock.each {|call| assert_equal :GrantBonus, call.name, "Should have been a GrantBonus call" }
78
+ end
79
+
80
+ def testTimeoutAlways
81
+ #mock will always timeout
82
+ @mock.listen do |call|
83
+ Timeout.timeout(1) { sleep(2) }
84
+ end
85
+
86
+ begin
87
+ @mturk.searchHITs
88
+ fail "Should have thrown an exception"
89
+ rescue Timeout::Error
90
+ # expect this exception
91
+ end
92
+
93
+ RETRYABLE_ATTEMPT_COUNT.times do
94
+ assert_equal :SearchHITs, @mock.next.name
95
+ end
96
+ assert_nil @mock.next
97
+ end
98
+
99
+ def testResetConnectionOnceRetryable
100
+ # mock will reset connection first time, return success on second call
101
+ should_fail = true
102
+ @mock.listen do |call|
103
+ if should_fail
104
+ should_fail = false
105
+ raise Errno::ECONNRESET
106
+ end
107
+ nil
108
+ end
109
+
110
+ # invoke a retryable call once, should auto-retry and return success
111
+ @mturk.getAccountBalance
112
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
113
+ @mock.each {|call| assert_equal :GetAccountBalance, call.name, "Should have been a GetAccountBalance call" }
114
+ end
115
+
116
+ def testResetConnectionOnceNotRetryable
117
+ # mock will reset connection first time, return success on second call
118
+ should_fail = true
119
+ @mock.listen do |call|
120
+ if should_fail
121
+ should_fail = false
122
+ raise Errno::ECONNRESET
123
+ end
124
+ nil
125
+ end
126
+
127
+ # invoking a non-retryable call will throw an exception
128
+ begin
129
+ @mturk.grantBonus
130
+ fail "Should have thrown an exception"
131
+ rescue Errno::ECONNRESET
132
+ # expect this exception
133
+ end
134
+ assert_equal :GrantBonus, @mock.next.name
135
+ assert_nil @mock.next
136
+ end
137
+
138
+ def testResetConnectionOnceRetryableUniqueRequestToken
139
+ # mock will reset connection first time, return success on second call
140
+ should_fail = true
141
+ @mock.listen do |call|
142
+ if should_fail
143
+ should_fail = false
144
+ raise Errno::ECONNRESET
145
+ end
146
+ nil
147
+ end
148
+
149
+ # invoke call with an idempotency once, should auto-retry and return success
150
+ @mturk.grantBonus :UniqueRequestToken => 'UniqueTokenValue'
151
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
152
+ @mock.each {|call| assert_equal :GrantBonus, call.name, "Should have been a GrantBonus call" }
153
+ end
154
+
155
+ def testResetConnectionAlways
156
+ #mock will always reset connection
157
+ @mock.listen do |call|
158
+ raise Errno::ECONNRESET
159
+ end
160
+
161
+ begin
162
+ @mturk.searchHITs
163
+ fail "Should have thrown an exception"
164
+ rescue Errno::ECONNRESET
165
+ # expect this exception
166
+ end
167
+
168
+ RETRYABLE_ATTEMPT_COUNT.times do
169
+ assert_equal :SearchHITs, @mock.next.name
170
+ end
171
+ assert_nil @mock.next
172
+ end
173
+
174
+ def testPipeBrokenOnceRetryable
175
+ # mock will break pipe first time, return success on second call
176
+ should_fail = true
177
+ @mock.listen do |call|
178
+ if should_fail
179
+ should_fail = false
180
+ raise Errno::EPIPE
181
+ end
182
+ nil
183
+ end
184
+
185
+ # invoke a retryable call once, should auto-retry and return success
186
+ @mturk.getAccountBalance
187
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
188
+ @mock.each {|call| assert_equal :GetAccountBalance, call.name, "Should have been a GetAccountBalance call" }
189
+ end
190
+
191
+ def testPipeBrokenOnceNotRetryable
192
+ # mock will break pipe first time, return success on second call
193
+ should_fail = true
194
+ @mock.listen do |call|
195
+ if should_fail
196
+ should_fail = false
197
+ raise Errno::EPIPE
198
+ end
199
+ nil
200
+ end
201
+
202
+ # invoking a non-retryable call will throw an exception
203
+ begin
204
+ @mturk.grantBonus
205
+ fail "Should have thrown an exception"
206
+ rescue Errno::EPIPE
207
+ # expect this exception
208
+ end
209
+ assert_equal :GrantBonus, @mock.next.name
210
+ assert_nil @mock.next
211
+ end
212
+
213
+ def testPipeBrokenOnceRetryableUniqueRequestToken
214
+ # mock will reset connection first time, return success on second call
215
+ should_fail = true
216
+ @mock.listen do |call|
217
+ if should_fail
218
+ should_fail = false
219
+ raise Errno::EPIPE
220
+ end
221
+ nil
222
+ end
223
+
224
+ # invoke call with an idempotency once, should auto-retry and return success
225
+ @mturk.grantBonus :UniqueRequestToken => 'UniqueTokenValue'
226
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
227
+ @mock.each {|call| assert_equal :GrantBonus, call.name, "Should have been a GrantBonus call" }
228
+ end
229
+
230
+ def testPipeBrokenAlways
231
+ #mock will always break pipe
232
+ @mock.listen do |call|
233
+ raise Errno::EPIPE
234
+ end
235
+
236
+ begin
237
+ @mturk.searchHITs
238
+ fail "Should have thrown an exception"
239
+ rescue Errno::EPIPE
240
+ # expect this exception
241
+ end
242
+
243
+ RETRYABLE_ATTEMPT_COUNT.times do
244
+ assert_equal :SearchHITs, @mock.next.name
245
+ end
246
+ assert_nil @mock.next
247
+ end
248
+
249
+ def testServiceUnavailableOnceRetryable
250
+ # mock will break pipe first time, return success on second call
251
+ should_fail = true
252
+ @mock.listen do |call|
253
+ if should_fail
254
+ should_fail = false
255
+ raise Amazon::WebServices::Util::ValidationException.new(nil, 'AWS.ServiceUnavailable')
256
+ end
257
+ nil
258
+ end
259
+
260
+ # invoke a retryable call once, should auto-retry and return success
261
+ @mturk.getAccountBalance
262
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
263
+ @mock.each {|call| assert_equal :GetAccountBalance, call.name, "Should have been a GetAccountBalance call" }
264
+ end
265
+
266
+ def testServiceUnavailableOnceNotRetryable
267
+ # mock will break pipe first time, return success on second call
268
+ should_fail = true
269
+ @mock.listen do |call|
270
+ if should_fail
271
+ should_fail = false
272
+ raise Amazon::WebServices::Util::ValidationException.new(nil, 'AWS.ServiceUnavailable')
273
+ end
274
+ nil
275
+ end
276
+
277
+ # invoking a non-retryable call will throw an exception
278
+ begin
279
+ @mturk.grantBonus
280
+ fail "Should have thrown an exception"
281
+ rescue Amazon::WebServices::Util::ValidationException
282
+ # expect this exception
283
+ end
284
+ assert_equal :GrantBonus, @mock.next.name
285
+ assert_nil @mock.next
286
+ end
287
+
288
+ def testServiceUnavailableOnceRetryableUniqueRequestToken
289
+ # mock will reset connection first time, return success on second call
290
+ should_fail = true
291
+ @mock.listen do |call|
292
+ if should_fail
293
+ should_fail = false
294
+ raise Amazon::WebServices::Util::ValidationException.new(nil, 'AWS.ServiceUnavailable')
295
+ end
296
+ nil
297
+ end
298
+
299
+ # invoke call with an idempotency once, should auto-retry and return success
300
+ @mturk.grantBonus :UniqueRequestToken => 'UniqueTokenValue'
301
+ assert_equal 2, @mock.call_buffer.size, "Should have retried once"
302
+ @mock.each {|call| assert_equal :GrantBonus, call.name, "Should have been a GrantBonus call" }
303
+ end
304
+
305
+ def testServiceUnavailableAlways
306
+ #mock will always break pipe
307
+ @mock.listen do |call|
308
+ raise Amazon::WebServices::Util::ValidationException.new(nil, 'AWS.ServiceUnavailable')
309
+ end
310
+
311
+ begin
312
+ @mturk.searchHITs
313
+ fail "Should have thrown an exception"
314
+ rescue Amazon::WebServices::Util::ValidationException
315
+ # expect this exception
316
+ end
317
+
318
+ RETRYABLE_ATTEMPT_COUNT.times do
319
+ assert_equal :SearchHITs, @mock.next.name
320
+ end
321
+ assert_nil @mock.next
322
+ end
323
+
324
+ def testGenericValidationException
325
+ @mock.listen do |call|
326
+ raise Amazon::WebServices::Util::ValidationException.new(nil, 'Blah blah')
327
+ end
328
+
329
+ begin
330
+ @mturk.searchHITs
331
+ fail "Should have thrown an exception"
332
+ rescue Amazon::WebServices::Util::ValidationException
333
+ # expect this exception
334
+ end
335
+ assert_equal :SearchHITs, @mock.next.name
336
+ assert_nil @mock.next
337
+ end
338
+
339
+ def testRuntimeError
340
+ @mock.listen do |call|
341
+ raise "Blah"
342
+ end
343
+
344
+ begin
345
+ @mturk.searchHITs
346
+ fail "Should have thrown an exception"
347
+ rescue RuntimeError => e
348
+ assert_equal "Blah", e.to_s
349
+ end
350
+ end
351
+
352
+ def testRuntimeErrorThrottled
353
+ @mock.listen do |call|
354
+ raise "Throttled"
355
+ end
356
+
357
+ begin
358
+ @mturk.searchHITs
359
+ fail "Should have thrown an exception"
360
+ rescue RuntimeError => e
361
+ assert_equal "Throttled", e.to_s
362
+ end
363
+ end
364
+
365
+ if Amazon::WebServices::Util::SOAPTransport.canSOAP?
366
+ def testSOAPFaultError
367
+ arg = (Struct.new :faultcode, :faultstring, :faultactor, :detail).new
368
+ arg.faultcode = (Struct.new :data).new 'aws:blarg'
369
+ arg.faultstring = (Struct.new :data).new 'blarg blarg blarg'
370
+ s = SOAP::FaultError.new( arg )
371
+
372
+ @mock.listen do |call|
373
+ raise s
374
+ end
375
+
376
+ begin
377
+ @mturk.searchHITs
378
+ fail "Should have thrown an exception"
379
+ rescue SOAP::FaultError => e
380
+ assert_equal s, e
381
+ end
382
+ end
383
+
384
+ def testSOAPFaultErrorThrottled
385
+ arg = (Struct.new :faultcode, :faultstring, :faultactor, :detail).new
386
+ arg.faultcode = (Struct.new :data).new 'aws:Server.ServiceUnavailable'
387
+ arg.faultstring = (Struct.new :data).new 'Hey, give us a break!'
388
+ s = SOAP::FaultError.new( arg )
389
+
390
+ @mock.listen do |call|
391
+ raise s
392
+ end
393
+
394
+ begin
395
+ @mturk.searchHITs
396
+ fail "Should have thrown an exception"
397
+ rescue SOAP::FaultError => e
398
+ assert_equal s, e
399
+ end
400
+ end
401
+ end # canSOAP?
402
+
403
+ end