Expect(os.RemoveAll(tmpDir)).To(Succeed()) client.Close() }) It("Writes and reads from a single file", func(done Done) { defer close(done) fileId, err := client.FetchFile("some-file") Expect(err).ToNot(HaveOccurred()) for i := byte(0); i < 100; i++ { _, err = client.WriteToFile(fileId, []byte{i}) Expect(err).ToNot(HaveOccurred()) } for i := 0; i < 100; i++ { data, err := client.ReadFromFile(fileId) Expect(err).ToNot(HaveOccurred()) Expect(data).To(HaveLen(1)) Expect(data[0]).To(BeEquivalentTo(i)) } }, 5) It("Writes and reads from a single file at the same time", func(done Done) { defer close(done) clientW := startClient(URL) clientR := startClient(URL) fileIdW, err := clientW.FetchFile("some-file") Expect(err).ToNot(HaveOccurred()) fileIdR, err := clientR.FetchFile("some-file") Expect(err).ToNot(HaveOccurred())
It("reads from the correct broker", func(done Done) { defer close(done) expectedData := []byte("some-data") mockServers[0].serverCh <- buildRemoteFileLocation(1, servers[1].URL) mockServers[1].serverCh <- buildFileLocation(1) mockServers[1].serverCh <- buildReadData(2, expectedData) id, ffErr := client.FetchFile("some-file-1") Expect(ffErr).ToNot(HaveOccurred()) var msg *messages.Client Eventually(mockServers[1].clientCh).Should(Receive(&msg)) Expect(msg.GetMessageType()).To(Equal(messages.Client_FetchFile)) Expect(msg.GetFetchFile().GetName()).To(Equal("some-file-1")) data, err := client.ReadFromFile(id) Expect(err).ToNot(HaveOccurred()) Expect(data).To(Equal(expectedData)) }) Measure("Reads from a file 1000 times in under a second", func(b Benchmarker) { runtime := b.Time("runtime", func() { mockServers[0].serverCh <- buildFileLocation(1) count := 1000 data := []byte("some-data") go func() { for i := 0; i < count; i++ { mockServers[0].serverCh <- buildReadData(uint64(i+2), data) } }()