6 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
7 "git.curoverse.com/arvados.git/sdk/go/keepclient"
16 type PullWorkerTestSuite struct{}
18 // Gocheck boilerplate
19 func TestPullWorker(t *testing.T) {
23 // Gocheck boilerplate
24 var _ = Suite(&PullWorkerTestSuite{})
26 var testPullLists map[string]string
27 var processedPullLists map[string]string
28 var readContent string
32 var currentTestData PullWorkerTestData
34 const READ_CONTENT = "Hi!"
36 func RunTestPullWorker(c *C) {
37 // Since keepstore does not come into picture in tests,
38 // we need to explicitly start the goroutine in tests.
39 arv, err := arvadosclient.MakeArvadosClient()
40 c.Assert(err, Equals, nil)
41 keepClient, err := keepclient.MakeKeepClient(&arv)
42 c.Assert(err, Equals, nil)
44 pullq = NewWorkQueue()
45 go RunPullWorker(pullq, keepClient)
48 func (s *PullWorkerTestSuite) SetUpTest(c *C) {
51 putContent = []byte("")
54 // When a new pull request arrives, the old one will be overwritten.
55 // This behavior is simulated with delay tests below.
56 testPullLists = make(map[string]string)
57 processedPullLists = make(map[string]string)
60 func (s *PullWorkerTestSuite) TearDownTest(c *C) {
61 time.Sleep(20 * time.Millisecond)
62 expectWorkerChannelEmpty(c, pullq.NextItem)
64 // give the channel some time to read and process all pull list entries
65 // time.Sleep(1000 * time.Millisecond)
66 // expectWorkerChannelEmpty(c, pullq.NextItem)
67 // c.Assert(len(processedPullLists), Not(Equals), len(testPullLists))
69 if currentTestData.read_error {
70 c.Assert(readError, NotNil)
72 c.Assert(readError, IsNil)
73 c.Assert(readContent, Equals, READ_CONTENT)
74 if currentTestData.put_error {
75 c.Assert(putError, NotNil)
77 c.Assert(putError, IsNil)
78 c.Assert(string(putContent), Equals, READ_CONTENT)
83 var first_pull_list = []byte(`[
99 var second_pull_list = []byte(`[
101 "locator":"locator3",
109 type PullWorkerTestData struct {
119 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_locators(c *C) {
122 data_manager_token = "DATA MANAGER TOKEN"
123 testData := PullWorkerTestData{
124 name: "TestPullWorker_pull_list_with_two_locators",
125 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
126 response_code: http.StatusOK,
127 response_body: "Received 2 pull requests\n",
128 read_content: "hello",
133 performTest(testData, c)
136 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator(c *C) {
139 data_manager_token = "DATA MANAGER TOKEN"
140 testData := PullWorkerTestData{
141 name: "TestPullWorker_pull_list_with_one_locator",
142 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
143 response_code: http.StatusOK,
144 response_body: "Received 1 pull requests\n",
145 read_content: "hola",
150 performTest(testData, c)
153 // When a new pull request arrives, the old one will be overwritten.
154 // Simulate this behavior by inducing delay in GetContent for the delay test(s).
155 // To ensure this delay test is not the last one executed and
156 // hence we cannot verify this behavior, let's run the delay test twice.
157 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator_with_delay_1(c *C) {
160 data_manager_token = "DATA MANAGER TOKEN"
161 testData := PullWorkerTestData{
162 name: "TestPullWorker_pull_list_with_one_locator_with_delay_1",
163 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
164 response_code: http.StatusOK,
165 response_body: "Received 1 pull requests\n",
166 read_content: "hola",
171 performTest(testData, c)
174 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator_with_delay_2(c *C) {
177 data_manager_token = "DATA MANAGER TOKEN"
178 testData := PullWorkerTestData{
179 name: "TestPullWorker_pull_list_with_one_locator_with_delay_2",
180 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
181 response_code: http.StatusOK,
182 response_body: "Received 1 pull requests\n",
183 read_content: "hola",
188 performTest(testData, c)
191 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_one_locator(c *C) {
194 data_manager_token = "DATA MANAGER TOKEN"
195 testData := PullWorkerTestData{
196 name: "TestPullWorker_error_on_get_one_locator",
197 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
198 response_code: http.StatusOK,
199 response_body: "Received 1 pull requests\n",
200 read_content: "unused",
205 performTest(testData, c)
208 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_two_locators(c *C) {
211 data_manager_token = "DATA MANAGER TOKEN"
212 testData := PullWorkerTestData{
213 name: "TestPullWorker_error_on_get_two_locators",
214 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
215 response_code: http.StatusOK,
216 response_body: "Received 2 pull requests\n",
217 read_content: "unused",
222 performTest(testData, c)
225 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_one_locator(c *C) {
228 data_manager_token = "DATA MANAGER TOKEN"
229 testData := PullWorkerTestData{
230 name: "TestPullWorker_error_on_put_one_locator",
231 req: RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
232 response_code: http.StatusOK,
233 response_body: "Received 1 pull requests\n",
234 read_content: "unused",
239 performTest(testData, c)
242 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
245 data_manager_token = "DATA MANAGER TOKEN"
246 testData := PullWorkerTestData{
247 name: "TestPullWorker_error_on_put_two_locators",
248 req: RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
249 response_code: http.StatusOK,
250 response_body: "Received 2 pull requests\n",
251 read_content: "unused",
256 performTest(testData, c)
259 func performTest(testData PullWorkerTestData, c *C) {
262 currentTestData = testData
263 testPullLists[testData.name] = testData.response_body
265 // We need to make sure the tests have a slight delay so that we can verify the pull list channel overwrites.
266 // time.Sleep(25 * time.Millisecond)
268 // Override GetContent to mock keepclient Get functionality
269 GetContent = func(signedLocator string, keepClient keepclient.KeepClient) (
270 reader io.ReadCloser, contentLength int64, url string, err error) {
271 if strings.HasPrefix(testData.name, "TestPullWorker_pull_list_with_one_locator_with_delay_1") {
272 // time.Sleep(100 * time.Millisecond)
275 processedPullLists[testData.name] = testData.response_body
276 if testData.read_error {
277 err = errors.New("Error getting data")
279 return nil, 0, "", err
281 readContent = READ_CONTENT
282 cb := &ClosingBuffer{bytes.NewBufferString(readContent)}
285 return rc, 3, "", nil
289 // Override PutContent to mock PutBlock functionality
290 PutContent = func(content []byte, locator string) (err error) {
291 if testData.put_error {
292 err = errors.New("Error putting data")
301 response := IssueRequest(&testData.req)
302 c.Assert(testData.response_code, Equals, response.Code)
303 c.Assert(testData.response_body, Equals, response.Body.String())
306 type ClosingBuffer struct {
310 func (cb *ClosingBuffer) Close() (err error) {
314 func expectWorkerChannelEmpty(c *C, workerChannel <-chan interface{}) {
316 case item := <-workerChannel:
317 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)