14 var testPullLists map[string]string
15 var processedPullLists map[string]string
17 type PullWorkerTestSuite struct{}
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
24 // Gocheck boilerplate
25 var _ = Suite(&PullWorkerTestSuite{})
27 func (s *PullWorkerTestSuite) SetUpSuite(c *C) {
28 // Since keepstore does not come into picture in tests,
29 // we need to explicitly start the goroutine in tests.
30 go RunPullWorker(pullq.NextItem)
32 // When a new pull request arrives, the old one will be overwritten.
33 // This behavior is simulated with delay tests below.
34 testPullLists = make(map[string]string)
35 processedPullLists = make(map[string]string)
38 func (s *PullWorkerTestSuite) TearDownSuite(c *C) {
39 // give the channel enough time to read and process all pull list entries
40 time.Sleep(1000 * time.Millisecond)
42 expectWorkerChannelEmpty(c, pullq.NextItem)
44 c.Assert(len(processedPullLists), Not(Equals), len(testPullLists))
47 var first_pull_list = []byte(`[
63 var second_pull_list = []byte(`[
73 type PullWorkerTestData struct {
83 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_two_locators(c *C) {
86 data_manager_token = "DATA MANAGER TOKEN"
87 testData := PullWorkerTestData{
88 "TestPullWorker_pull_list_with_two_locators",
89 RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
91 "Received 2 pull requests\n",
97 performTest(testData, c)
100 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator(c *C) {
103 data_manager_token = "DATA MANAGER TOKEN"
104 testData := PullWorkerTestData{
105 "TestPullWorker_pull_list_with_one_locator",
106 RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
108 "Received 1 pull requests\n",
114 performTest(testData, c)
117 // When a new pull request arrives, the old one will be overwritten.
118 // Simulate this behavior by inducing delay in GetContent for the delay test(s).
119 // To ensure this delay test is not the last one executed and
120 // hence we cannot verify this behavior, let's run the delay test twice.
121 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator_with_delay_1(c *C) {
124 data_manager_token = "DATA MANAGER TOKEN"
125 testData := PullWorkerTestData{
126 "TestPullWorker_pull_list_with_one_locator_with_delay_1",
127 RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
129 "Received 1 pull requests\n",
135 performTest(testData, c)
138 func (s *PullWorkerTestSuite) TestPullWorker_pull_list_with_one_locator_with_delay_2(c *C) {
141 data_manager_token = "DATA MANAGER TOKEN"
142 testData := PullWorkerTestData{
143 "TestPullWorker_pull_list_with_one_locator_with_delay_2",
144 RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
146 "Received 1 pull requests\n",
152 performTest(testData, c)
155 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_one_locator(c *C) {
158 data_manager_token = "DATA MANAGER TOKEN"
159 testData := PullWorkerTestData{
160 "TestPullWorker_error_on_get_one_locator",
161 RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
163 "Received 1 pull requests\n",
169 performTest(testData, c)
172 func (s *PullWorkerTestSuite) TestPullWorker_error_on_get_two_locators(c *C) {
175 data_manager_token = "DATA MANAGER TOKEN"
176 testData := PullWorkerTestData{
177 "TestPullWorker_error_on_get_two_locators",
178 RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
180 "Received 2 pull requests\n",
186 performTest(testData, c)
189 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_one_locator(c *C) {
192 data_manager_token = "DATA MANAGER TOKEN"
193 testData := PullWorkerTestData{
194 "TestPullWorker_error_on_put_one_locator",
195 RequestTester{"/pull", data_manager_token, "PUT", second_pull_list},
197 "Received 1 pull requests\n",
203 performTest(testData, c)
206 func (s *PullWorkerTestSuite) TestPullWorker_error_on_put_two_locators(c *C) {
209 data_manager_token = "DATA MANAGER TOKEN"
210 testData := PullWorkerTestData{
211 "TestPullWorker_error_on_put_two_locators",
212 RequestTester{"/pull", data_manager_token, "PUT", first_pull_list},
214 "Received 2 pull requests\n",
220 performTest(testData, c)
223 func performTest(testData PullWorkerTestData, c *C) {
224 testPullLists[testData.name] = testData.response_body
226 // We need to make sure the tests have a slight delay so that we can verify the pull list channel overwrites.
227 time.Sleep(25 * time.Millisecond)
229 // Override GetContent to mock keepclient functionality
230 GetContent = func(locator string, signedLocator string) (reader io.ReadCloser, contentLength int64, url string, err error) {
231 if strings.HasPrefix(testData.name, "TestPullWorker_pull_list_with_one_locator_with_delay") {
232 time.Sleep(100 * time.Millisecond)
235 processedPullLists[testData.name] = testData.response_body
236 if testData.read_error {
237 return nil, 0, "", errors.New("Error getting data")
239 cb := &ClosingBuffer{bytes.NewBufferString("Hi!")}
242 return rc, 3, "", nil
246 // Override PutContent to mock PutBlock functionality
247 PutContent = func(content []byte, locator string) (err error) {
248 if testData.put_error {
249 return errors.New("Error putting data")
255 response := IssueRequest(&testData.req)
256 c.Assert(testData.response_code, Equals, response.Code)
257 c.Assert(testData.response_body, Equals, response.Body.String())
260 type ClosingBuffer struct {
264 func (cb *ClosingBuffer) Close() (err error) {
268 func expectWorkerChannelEmpty(c *C, workerChannel <-chan interface{}) {
270 case item := <-workerChannel:
271 c.Fatalf("Received value (%v) from channel that was expected to be empty", item)