Beispiel #1
0
// Not a test, but go test won't run it otherwise.
func TestStartServer(t *testing.T) {
	var err error
	server, err = testutils.NewZkServer()
	if err != nil {
		t.Fatalf("cannot start zk server")
	}

	zkConn, err = testutils.NewZkConn(server, true)
	if err != nil {
		t.Fatalf("cannot create connection")
	}

	_, err = zkConn.Conn.Create(zkRoot, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		t.Fatalf("cannot create zk root")
	}

	for _, node := range []string{"pools", "rules", "tries", "ports"} {
		zkPath := path.Join(zkRoot, node)
		_, err = zkConn.Conn.Create(zkPath, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil {
			t.Fatalf("cannot create node %s", node)
		}
	}

	SetZkRoot(zkRoot)
}
func TestManagedZkConn(t *testing.T) {
	// NOTE This test will take ~20s. If that is unacceptably long
	// for your development purposes, uncomment the next 4 lines.
	//
	// if !testing.Verbose() {
	// 	t.Skipf("skipping connection test, use verbose to run")
	// }

	server, err := testutils.NewZkServer()
	if err != nil {
		t.Fatalf("cannot start zk server")
	}
	defer server.Destroy()

	addr, _ := server.Addr()
	conn := ManagedZkConn(addr)
	defer conn.Shutdown()

	<-conn.ResetCh
	server.Stop()
	server.Start()

	select {
	case <-conn.ResetCh:
		t.Logf("recieved reset on channel")
	case <-time.After(30 * time.Second):
		t.Errorf("connection did not reset")
	}
}
func TestManageTree(t *testing.T) {
	server, err := testutils.NewZkServer()
	if err != nil {
		t.Fatalf("cannot start zk server")
	}
	addr, _ := server.Addr()

	numGoRoutine := runtime.NumGoroutine()

	zk := ManagedZkConn(addr)
	<-zk.ResetCh
	zk.Conn.Create("/testing", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	setupList := []string{
		"/testing/setup0",
		"/testing/setup1",
	}
	for _, node := range setupList {
		zk.Conn.Create(node, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}

	track := TrackingCallbacks{
		Track: make(map[string]*Track),
	}
	go zk.ManageTree("/testing", track, track, track)
	time.Sleep(100 * time.Millisecond)

	if len(track.Track) != 2 {
		t.Errorf("should be tracking 2 nodes")
	} else if !track.tally(setupList, StatusCreated) {
		t.Errorf("setupList should have status created")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	level0 := []string{
		"/testing/node0",
		"/testing/node1",
		"/testing/node2",
	}
	for _, node := range level0 {
		go zk.Conn.Create(node, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}
	time.Sleep(100 * time.Millisecond)

	if len(track.Track) != 5 {
		t.Errorf("should be tracking 2 nodes")
	} else if !track.tally(level0, StatusCreated) {
		t.Errorf("level0 should have status created")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	level1 := []string{
		"/testing/node0/node0",
		"/testing/node0/node1",
		"/testing/node0/node2",
	}
	for _, node := range level1 {
		go zk.Conn.Create(node, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}
	time.Sleep(100 * time.Millisecond)

	if len(track.Track) != 8 {
		t.Errorf("should be tracking 6 nodes")
	} else if !track.tally(level0, StatusCreated) {
		t.Errorf("level0 should have status created")
	} else if !track.tally(level1, StatusCreated) {
		t.Errorf("level1 should have status created")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	level2 := []string{
		"/testing/node0/node0/node0",
		"/testing/node0/node0/node1",
	}
	for _, node := range level2 {
		go zk.Conn.Create(node, "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	}
	time.Sleep(100 * time.Millisecond)

	if len(track.Track) != 10 {
		t.Errorf("should be tracking 8 nodes")
	} else if !track.tally(level0, StatusCreated) {
		t.Errorf("level0 should have status created")
	} else if !track.tally(level1, StatusCreated) {
		t.Errorf("level1 should have status created")
	} else if !track.tally(level2, StatusCreated) {
		t.Errorf("level2 should have status created")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	changeList := []string{
		"/testing/node1",
		"/testing/node0/node0",
		"/testing/node0/node0/node1",
	}
	for _, node := range changeList {
		go zk.Conn.Set(node, "changed!", -1)
	}
	time.Sleep(100 * time.Millisecond)

	if !track.tally(changeList, StatusChanged) {
		t.Errorf("changeList should have status changed")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	deleteList := []string{
		"/testing/node0/node0/node0",
		"/testing/node0/node0/node1",
		"/testing/node0/node0",
	}
	for _, node := range deleteList {
		zk.Conn.Delete(node, -1)
	}

	if !track.tally(deleteList, StatusDeleted) {
		t.Errorf("deleteList should have status deleted")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	changeList2 := []string{
		"/testing/node0/node1",
		"/testing/node2",
	}
	for _, node := range changeList2 {
		go zk.Conn.Set(node, "again!", -1)
	}
	time.Sleep(100 * time.Millisecond)

	if !track.tally(changeList2, StatusChanged) {
		t.Errorf("changeList2 should have status changed")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	zk.Conn.Create("/testing/node0/node0", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	time.Sleep(100 * time.Millisecond)

	if !track.tally([]string{"/testing/node0/node0"}, StatusCreated) {
		t.Errorf("/testing/node0/node0 should have status created")
	}
	if !track.valid() {
		t.Errorf("should be valid")
	}

	zk.Shutdown()

	// This test consistently fails under gocov and passes under
	// go test. Switch false to true if you need to verify that
	// ManageTree is not leaking goroutines.
	if false {
		time.Sleep(100 * time.Millisecond)
		if runtime.NumGoroutine() != numGoRoutine {
			t.Errorf("should not leak go routines")
		}
	}

	server.Destroy()
}