Merge branch '8784-dir-listings'
[arvados.git] / services / keepstore / s3_volume_test.go
index 702e5539dfb0f060360120bc4312ce391b523b0c..c2084eea8d58718f98f223c8380320e9d8e80bf2 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 import (
@@ -129,10 +133,35 @@ func (h *blockingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        http.Error(w, "nothing here", http.StatusNotFound)
 }
 
-func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
+func (s *StubbedS3Suite) TestGetContextCancel(c *check.C) {
        loc := "acbd18db4cc2f85cedef654fccc4a4d8"
        buf := make([]byte, 3)
 
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               _, err := v.Get(ctx, loc, buf)
+               return err
+       })
+}
+
+func (s *StubbedS3Suite) TestCompareContextCancel(c *check.C) {
+       loc := "acbd18db4cc2f85cedef654fccc4a4d8"
+       buf := []byte("bar")
+
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               return v.Compare(ctx, loc, buf)
+       })
+}
+
+func (s *StubbedS3Suite) TestPutContextCancel(c *check.C) {
+       loc := "acbd18db4cc2f85cedef654fccc4a4d8"
+       buf := []byte("foo")
+
+       s.testContextCancel(c, func(ctx context.Context, v *TestableS3Volume) error {
+               return v.Put(ctx, loc, buf)
+       })
+}
+
+func (s *StubbedS3Suite) testContextCancel(c *check.C, testFunc func(context.Context, *TestableS3Volume) error) {
        handler := &blockingHandler{}
        srv := httptest.NewServer(handler)
        defer srv.Close()
@@ -149,12 +178,11 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        handler.unblock = make(chan struct{})
        defer close(handler.unblock)
 
-       var n int
-       var err error
-       doneGet := make(chan struct{})
+       doneFunc := make(chan struct{})
        go func() {
-               n, err = v.Get(ctx, loc, buf)
-               close(doneGet)
+               err := testFunc(ctx, v)
+               c.Check(err, check.Equals, context.Canceled)
+               close(doneFunc)
        }()
 
        timeout := time.After(10 * time.Second)
@@ -163,9 +191,9 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        // Get() is waiting for an s3 operation.
        select {
        case <-timeout:
-               c.Fatal("timed out waiting for Get to call our handler")
-       case <-doneGet:
-               c.Fatal("Get finished without calling our handler!")
+               c.Fatal("timed out waiting for test func to call our handler")
+       case <-doneFunc:
+               c.Fatal("test func finished without even calling our handler!")
        case <-handler.requested:
        }
 
@@ -174,10 +202,7 @@ func (s *StubbedS3Suite) TestClientDisconnect(c *check.C) {
        select {
        case <-timeout:
                c.Fatal("timed out")
-       case <-doneGet:
-               c.Check(n, check.Equals, 0)
-               c.Check(err, check.NotNil)
-               c.Check(err, check.Equals, context.Canceled)
+       case <-doneFunc:
        }
 }