Beispiel #1
0
func TestJoinUpdatesExistingPeers(t *testing.T) {
	test := quiz.Test(t)

	nodeA := testNode()
	defer nodeA.Close()
	nodeB := testNode()
	defer nodeB.Close()
	nodeC := testNode()
	defer nodeC.Close()

	httpclient.Put(nodeA.URL+"/peers/join", nodeB.URL)
	httpclient.Put(nodeA.URL+"/peers/join", nodeC.URL)

	var statusCode int
	var body string

	statusCode, body = httpclient.Get(nodeA.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToContain(nodeB.URL)
	test.Expect(body).ToContain(nodeC.URL)

	statusCode, body = httpclient.Get(nodeB.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToContain(nodeA.URL)
	test.Expect(body).ToContain(nodeC.URL)

	statusCode, body = httpclient.Get(nodeC.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToContain(nodeA.URL)
	test.Expect(body).ToContain(nodeB.URL)
}
Beispiel #2
0
func TestFetchesAcrossNodes(t *testing.T) {
	test := quiz.Test(t)

	serverA := testServer()
	defer serverA.Close()
	serverB := testServer()
	defer serverB.Close()

	httpclient.Put(serverA.URL+"/peers/join", serverB.URL)

	// "a"'s hash will be stored on serverB
	key := "a"

	var statusCode int
	var body string

	statusCode, _ = httpclient.Put(serverA.URL+"/data/"+key, "bar")
	test.Expect(statusCode).ToEqual(201)

	statusCode, body = httpclient.Get(serverB.URL+"/data/"+key, "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual("bar")

	statusCode, body = httpclient.Get(serverA.URL+"/data/"+key, "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual("bar")
}
Beispiel #3
0
func TestUpdateKey(t *testing.T) {
	test := quiz.Test(t)

	server := testServer()
	defer server.Close()

	httpclient.Put(server.URL+"/data/mykey", "bar")
	httpclient.Put(server.URL+"/data/mykey", "baz")
	statusCode, body := httpclient.Get(server.URL+"/data/mykey", "")

	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual("baz")
}
Beispiel #4
0
func TestAddPeerFailsOnMultipleCalls(t *testing.T) {
	test := quiz.Test(t)

	node := testNode()
	defer node.Close()

	var statusCode int

	statusCode, _ = httpclient.Put(node.URL+"/peers", "peer.url")
	test.Expect(statusCode).ToEqual(201)

	statusCode, _ = httpclient.Put(node.URL+"/peers", "peer.url")
	test.Expect(statusCode).ToEqual(409)
}
Beispiel #5
0
func TestGetMultiplePeers(t *testing.T) {
	test := quiz.Test(t)

	node := testNode()
	defer node.Close()

	httpclient.Put(node.URL+"/peers", "peer1.url")
	httpclient.Put(node.URL+"/peers", "peer2.url")

	statusCode, body := httpclient.Get(node.URL+"/peers", "")

	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual(`{"peers":["peer1.url","peer2.url"]}`)
}
Beispiel #6
0
func (peer *Peer) join(newPeer string) {
	for _, p := range peer.Peers {
		httpclient.Put(p+"/peers", newPeer)
		httpclient.Put(newPeer+"/peers", p)
	}

	peer.addPeer(newPeer)
	peer.ring.AddNode(newPeer)
	httpclient.Put(newPeer+"/peers", peer.url)

	for _, p := range peer.Peers {
		nodes := httpclient.JsonData{
			"ring": peer.ring.GetNodes(),
		}
		httpclient.Put(p+"/ring", nodes.Encode())
	}
}
Beispiel #7
0
func TestNodeSetRing(t *testing.T) {
	test := quiz.Test(t)

	node := testNode()
	defer node.Close()

	statusCode, _ := httpclient.Put(node.URL+"/ring", `{"ring":["`+node.URL+`","b","c"]}`)
	test.Expect(statusCode).ToEqual(201)

	_, body := httpclient.Get(node.URL+"/stats", "")
	test.Expect(body).ToContain(`"ring":["` + node.URL + `","b","c"]`)
}
Beispiel #8
0
func TestAddPeer(t *testing.T) {
	test := quiz.Test(t)

	node := testNode()
	defer node.Close()

	statusCode, _ := httpclient.Put(node.URL+"/peers", "peer.url")
	test.Expect(statusCode).ToEqual(201)

	statusCode, body := httpclient.Get(node.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual(`{"peers":["peer.url"]}`)
}
Beispiel #9
0
func (server *Server) Handler() http.Handler {
	m := pat.New()

	server.Peer.Handler(m)

	m.Put("/data/:key", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		key := request.URL.Query().Get(":key")
		body, _ := ioutil.ReadAll(request.Body)
		value := string(body)

		destinationAddress := server.PeerAddressForKey(key)

		if server.URL() == destinationAddress {
			server.logger.Printf("Storing '%s'->'%s'", key, value)
			server.values[key] = value
			w.WriteHeader(201)
			io.WriteString(w, value)
		} else {
			server.logger.Printf("Passing off '%s'->'%s' to %s", key, value, destinationAddress)
			statusCode, response := httpclient.Put(destinationAddress+"/data/"+key, value)
			w.WriteHeader(statusCode)
			io.WriteString(w, response)
		}
	}))

	m.Get("/data/:key", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		key := request.URL.Query().Get(":key")

		destinationAddress := server.PeerAddressForKey(key)

		if server.URL() == destinationAddress {
			value, ok := server.values[key]
			if ok {
				w.WriteHeader(200)
				io.WriteString(w, value)
			} else {
				w.WriteHeader(404)
			}
		} else {
			statusCode, response := httpclient.Get(destinationAddress+"/data/"+key, "")
			w.WriteHeader(statusCode)
			io.WriteString(w, response)
		}
	}))

	return m
}
Beispiel #10
0
func TestJoinCallsBack(t *testing.T) {
	test := quiz.Test(t)

	nodeA := testNode()
	defer nodeA.Close()
	nodeB := testNode()
	defer nodeB.Close()

	httpclient.Put(nodeA.URL+"/peers/join", nodeB.URL)

	var statusCode int
	var body string

	statusCode, body = httpclient.Get(nodeA.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual(`{"peers":["` + nodeB.URL + `"]}`)

	statusCode, body = httpclient.Get(nodeB.URL+"/peers", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToEqual(`{"peers":["` + nodeA.URL + `"]}`)
}
Beispiel #11
0
func TestAddNodeUpdatesRing(t *testing.T) {
	test := quiz.Test(t)
	var statusCode int
	var body string

	nodeA := testNode()
	defer nodeA.Close()

	nodeB := testNode()
	defer nodeB.Close()

	httpclient.Put(nodeA.URL+"/peers/join", nodeB.URL)

	statusCode, body = httpclient.Get(nodeA.URL+"/stats", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToContain(`"vnodeCount":512`)
	test.Expect(body).ToContain(`"vnodeStart":0`)

	statusCode, body = httpclient.Get(nodeB.URL+"/stats", "")
	test.Expect(statusCode).ToEqual(200)
	test.Expect(body).ToContain(`"vnodeCount":512`)
	test.Expect(body).ToContain(`"vnodeStart":2147483137`)
}