Added comment, ran gofmt.
[arvados.git] / sdk / go / keepclient / keepclient_test.go
index c958695429ad34d4e01dcb128ac97ab02fa218d9..8487e00786d93d4acece1fcf83c065629e499944 100644 (file)
@@ -1,11 +1,11 @@
 package keepclient
 
 import (
-       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
-       "git.curoverse.com/arvados.git/sdk/go/streamer"
        "crypto/md5"
        "flag"
        "fmt"
+       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
+       "git.curoverse.com/arvados.git/sdk/go/streamer"
        . "gopkg.in/check.v1"
        "io"
        "io/ioutil"
@@ -92,29 +92,6 @@ func (s *ServerRequiredSuite) TestMakeKeepClient(c *C) {
        }
 }
 
-func (s *StandaloneSuite) TestShuffleServiceRoots(c *C) {
-       roots := map[string]string{
-               "zzzzz-bi6l4-2q7dq8becevdqfb": "http://localhost:1",
-               "zzzzz-bi6l4-4gbhck2w7lq0d96": "http://localhost:2",
-               "zzzzz-bi6l4-4bt69dsk0quh7ae": "http://localhost:3",
-               "zzzzz-bi6l4-62w1fgd0ud2krxl": "http://localhost:4",
-       }
-       kc := KeepClient{}
-       kc.SetServiceRoots(roots)
-
-       // "foo" acbd18db4cc2f85cedef654fccc4a4d8
-       foo_shuffle := []string{"http://localhost:4", "http://localhost:1", "http://localhost:3", "http://localhost:2"}
-       c.Check(NewRootSorter(
-               kc.ServiceRoots(), Md5String("foo")).GetSortedRoots(),
-               DeepEquals, foo_shuffle)
-
-       // "bar" 37b51d194a7513e45b56f6524f2d51f2
-       bar_shuffle := []string{"http://localhost:3", "http://localhost:2", "http://localhost:4", "http://localhost:1"}
-       c.Check(NewRootSorter(
-               kc.ServiceRoots(), Md5String("bar")).GetSortedRoots(),
-               DeepEquals, bar_shuffle)
-}
-
 type StubPutHandler struct {
        c              *C
        expectPath     string
@@ -133,24 +110,22 @@ func (this StubPutHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request
        this.handled <- fmt.Sprintf("http://%s", req.Host)
 }
 
-func RunBogusKeepServer(st http.Handler, port int) (listener net.Listener, url string) {
+func RunFakeKeepServer(st http.Handler) (ks KeepServer) {
        var err error
-       listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: port})
+       ks.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
        if err != nil {
-               panic(fmt.Sprintf("Could not listen on tcp port %v", port))
+               panic(fmt.Sprintf("Could not listen on any port"))
        }
-
-       url = fmt.Sprintf("http://localhost:%d", port)
-
-       go http.Serve(listener, st)
-       return listener, url
+       ks.url = fmt.Sprintf("http://%s", ks.listener.Addr().String())
+       go http.Serve(ks.listener, st)
+       return
 }
 
 func UploadToStubHelper(c *C, st http.Handler, f func(KeepClient, string,
        io.ReadCloser, io.WriteCloser, chan uploadStatus)) {
 
-       listener, url := RunBogusKeepServer(st, 2990)
-       defer listener.Close()
+       ks := RunFakeKeepServer(st)
+       defer ks.listener.Close()
 
        arv, _ := arvadosclient.MakeArvadosClient()
        arv.ApiToken = "abc123"
@@ -160,7 +135,7 @@ func UploadToStubHelper(c *C, st http.Handler, f func(KeepClient, string,
        reader, writer := io.Pipe()
        upload_status := make(chan uploadStatus)
 
-       f(kc, url, reader, writer, upload_status)
+       f(kc, ks.url, reader, writer, upload_status)
 }
 
 func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
@@ -177,7 +152,7 @@ func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
                func(kc KeepClient, url string, reader io.ReadCloser,
                        writer io.WriteCloser, upload_status chan uploadStatus) {
 
-                       go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")))
+                       go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")), "TestUploadToStubKeepServer")
 
                        writer.Write([]byte("foo"))
                        writer.Close()
@@ -209,7 +184,7 @@ func (s *StandaloneSuite) TestUploadToStubKeepServerBufferReader(c *C) {
 
                        br1 := tr.MakeStreamReader()
 
-                       go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3)
+                       go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3, "TestUploadToStubKeepServerBufferReader")
 
                        writer.Write([]byte("foo"))
                        writer.Close()
@@ -244,7 +219,7 @@ func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
                func(kc KeepClient, url string, reader io.ReadCloser,
                        writer io.WriteCloser, upload_status chan uploadStatus) {
 
-                       go kc.uploadToKeepServer(url, hash, reader, upload_status, 3)
+                       go kc.uploadToKeepServer(url, hash, reader, upload_status, 3, "TestFailedUploadToStubKeepServer")
 
                        writer.Write([]byte("foo"))
                        writer.Close()
@@ -263,12 +238,11 @@ type KeepServer struct {
        url      string
 }
 
-func RunSomeFakeKeepServers(st http.Handler, n int, port int) (ks []KeepServer) {
+func RunSomeFakeKeepServers(st http.Handler, n int) (ks []KeepServer) {
        ks = make([]KeepServer, n)
 
        for i := 0; i < n; i += 1 {
-               boguslistener, bogusurl := RunBogusKeepServer(st, port+i)
-               ks[i] = KeepServer{boguslistener, bogusurl}
+               ks[i] = RunFakeKeepServer(st)
        }
 
        return ks
@@ -293,11 +267,11 @@ func (s *StandaloneSuite) TestPutB(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks := RunSomeFakeKeepServers(st, 5, 2990)
+       ks := RunSomeFakeKeepServers(st, 5)
 
-       for i := 0; i < len(ks); i += 1 {
-               service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = ks[i].url
-               defer ks[i].listener.Close()
+       for i, k := range ks {
+               service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
+               defer k.listener.Close()
        }
 
        kc.SetServiceRoots(service_roots)
@@ -336,11 +310,11 @@ func (s *StandaloneSuite) TestPutHR(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks := RunSomeFakeKeepServers(st, 5, 2990)
+       ks := RunSomeFakeKeepServers(st, 5)
 
-       for i := 0; i < len(ks); i += 1 {
-               service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = ks[i].url
-               defer ks[i].listener.Close()
+       for i, k := range ks {
+               service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
+               defer k.listener.Close()
        }
 
        kc.SetServiceRoots(service_roots)
@@ -390,8 +364,8 @@ func (s *StandaloneSuite) TestPutWithFail(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks1 := RunSomeFakeKeepServers(st, 4, 2990)
-       ks2 := RunSomeFakeKeepServers(fh, 1, 2995)
+       ks1 := RunSomeFakeKeepServers(st, 4)
+       ks2 := RunSomeFakeKeepServers(fh, 1)
 
        for i, k := range ks1 {
                service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
@@ -414,8 +388,14 @@ func (s *StandaloneSuite) TestPutWithFail(c *C) {
        c.Check(err, Equals, nil)
        c.Check(phash, Equals, "")
        c.Check(replicas, Equals, 2)
-       c.Check(<-st.handled, Equals, shuff[1])
-       c.Check(<-st.handled, Equals, shuff[2])
+
+       s1 := <-st.handled
+       s2 := <-st.handled
+
+       c.Check((s1 == shuff[1] && s2 == shuff[2]) ||
+               (s1 == shuff[2] && s2 == shuff[1]),
+               Equals,
+               true)
 }
 
 func (s *StandaloneSuite) TestPutWithTooManyFail(c *C) {
@@ -440,8 +420,8 @@ func (s *StandaloneSuite) TestPutWithTooManyFail(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks1 := RunSomeFakeKeepServers(st, 1, 2990)
-       ks2 := RunSomeFakeKeepServers(fh, 4, 2991)
+       ks1 := RunSomeFakeKeepServers(st, 1)
+       ks2 := RunSomeFakeKeepServers(fh, 4)
 
        for i, k := range ks1 {
                service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
@@ -458,7 +438,7 @@ func (s *StandaloneSuite) TestPutWithTooManyFail(c *C) {
 
        c.Check(err, Equals, InsufficientReplicasError)
        c.Check(replicas, Equals, 1)
-       c.Check(<-st.handled, Matches, ".*2990")
+       c.Check(<-st.handled, Equals, ks1[0].url)
 
        log.Printf("TestPutWithTooManyFail done")
 }
@@ -488,19 +468,19 @@ func (s *StandaloneSuite) TestGet(c *C) {
                "abc123",
                []byte("foo")}
 
-       listener, url := RunBogusKeepServer(st, 2990)
-       defer listener.Close()
+       ks := RunFakeKeepServer(st)
+       defer ks.listener.Close()
 
        arv, err := arvadosclient.MakeArvadosClient()
        kc, _ := MakeKeepClient(&arv)
        arv.ApiToken = "abc123"
-       kc.SetServiceRoots(map[string]string{"x":url})
+       kc.SetServiceRoots(map[string]string{"x": ks.url})
 
        r, n, url2, err := kc.Get(hash)
        defer r.Close()
        c.Check(err, Equals, nil)
        c.Check(n, Equals, int64(3))
-       c.Check(url2, Equals, fmt.Sprintf("%s/%s", url, hash))
+       c.Check(url2, Equals, fmt.Sprintf("%s/%s", ks.url, hash))
 
        content, err2 := ioutil.ReadAll(r)
        c.Check(err2, Equals, nil)
@@ -514,13 +494,13 @@ func (s *StandaloneSuite) TestGetFail(c *C) {
 
        st := FailHandler{make(chan string, 1)}
 
-       listener, url := RunBogusKeepServer(st, 2990)
-       defer listener.Close()
+       ks := RunFakeKeepServer(st)
+       defer ks.listener.Close()
 
        arv, err := arvadosclient.MakeArvadosClient()
        kc, _ := MakeKeepClient(&arv)
        arv.ApiToken = "abc123"
-       kc.SetServiceRoots(map[string]string{"x":url})
+       kc.SetServiceRoots(map[string]string{"x": ks.url})
 
        r, n, url2, err := kc.Get(hash)
        c.Check(err, Equals, BlockNotFound)
@@ -544,13 +524,13 @@ func (s *StandaloneSuite) TestChecksum(c *C) {
 
        st := BarHandler{make(chan string, 1)}
 
-       listener, url := RunBogusKeepServer(st, 2990)
-       defer listener.Close()
+       ks := RunFakeKeepServer(st)
+       defer ks.listener.Close()
 
        arv, err := arvadosclient.MakeArvadosClient()
        kc, _ := MakeKeepClient(&arv)
        arv.ApiToken = "abc123"
-       kc.SetServiceRoots(map[string]string{"x":url})
+       kc.SetServiceRoots(map[string]string{"x": ks.url})
 
        r, n, _, err := kc.Get(barhash)
        _, err = ioutil.ReadAll(r)
@@ -585,8 +565,8 @@ func (s *StandaloneSuite) TestGetWithFailures(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks1 := RunSomeFakeKeepServers(st, 1, 2990)
-       ks2 := RunSomeFakeKeepServers(fh, 4, 2991)
+       ks1 := RunSomeFakeKeepServers(st, 1)
+       ks2 := RunSomeFakeKeepServers(fh, 4)
 
        for i, k := range ks1 {
                service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
@@ -605,7 +585,7 @@ func (s *StandaloneSuite) TestGetWithFailures(c *C) {
        // the choice of block content "waz" and the UUIDs of the fake
        // servers, so we just tried different strings until we found
        // an example that passes this Assert.)
-       c.Assert(NewRootSorter(service_roots, hash).GetSortedRoots()[0], Matches, ".*299[1-4]")
+       c.Assert(NewRootSorter(service_roots, hash).GetSortedRoots()[0], Not(Equals), ks1[0].url)
 
        r, n, url2, err := kc.Get(hash)
 
@@ -682,7 +662,7 @@ func (s *StandaloneSuite) TestPutProxy(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks1 := RunSomeFakeKeepServers(st, 1, 2990)
+       ks1 := RunSomeFakeKeepServers(st, 1)
 
        for i, k := range ks1 {
                service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
@@ -713,7 +693,7 @@ func (s *StandaloneSuite) TestPutProxyInsufficientReplicas(c *C) {
        arv.ApiToken = "abc123"
        service_roots := make(map[string]string)
 
-       ks1 := RunSomeFakeKeepServers(st, 1, 2990)
+       ks1 := RunSomeFakeKeepServers(st, 1)
 
        for i, k := range ks1 {
                service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url