Пример #1
0
func CheckServerConcur(t *testing.T, server trib.Server) {
	runtime.GOMAXPROCS(2)

	ne := func(e error) {
		if e != nil {
			debug.PrintStack()
			t.Fatal(e)
		}
	}

	er := func(e error) {
		if e == nil {
			debug.PrintStack()
			t.Fatal()
		}
	}

	as := func(cond bool) {
		if !cond {
			debug.PrintStack()
			t.Fatal()
		}
	}

	ne(server.SignUp("user"))

	p := func(th, n int, done chan<- bool) {
		for i := 0; i < n; i++ {
			ne(server.Post("user", strconv.Itoa(th*100+n), 0))
		}
		done <- true
	}

	nconcur := 9
	done := make(chan bool, nconcur)
	for i := 0; i < nconcur; i++ {
		go p(i, 10, done)
	}

	for i := 0; i < nconcur; i++ {
		<-done
	}

	ret, e := server.Tribs("user")
	ne(e)
	as(len(ret) == 10*nconcur || len(ret) == trib.MaxTribFetch)

	ne(server.SignUp("other"))
	fo := func(done chan<- bool) {
		e := server.Follow("user", "other")
		done <- (e == nil)
	}

	unfo := func(done chan<- bool) {
		e := server.Unfollow("user", "other")
		done <- (e == nil)
	}

	for i := 0; i < nconcur; i++ {
		go fo(done)
	}
	cnt := 0
	for i := 0; i < nconcur; i++ {
		if <-done {
			cnt++
		}
	}
	t.Logf("%d follows", cnt)
	// as(cnt == 1)

	er(server.Follow("user", "other"))

	fos, e := server.Following("user")
	ne(e)
	as(len(fos) == 1)
	as(fos[0] == "other")

	for i := 0; i < nconcur; i++ {
		go unfo(done)
	}
	cnt = 0
	for i := 0; i < nconcur; i++ {
		if <-done {
			cnt++
		}
	}
	t.Logf("%d unfollows", cnt)
	// as(cnt == 1)

	fos, e = server.Following("user")
	ne(e)
	as(len(fos) == 0)
}
Пример #2
-1
func CheckServer(t *testing.T, server trib.Server) {
	ne := func(e error) {
		if e != nil {
			debug.PrintStack()
			t.Fatal(e)
		}
	}

	er := func(e error) {
		if e == nil {
			debug.PrintStack()
			t.Fatal(e)
		}
	}

	as := func(cond bool) {
		if !cond {
			debug.PrintStack()
			t.Fatal()
		}
	}

	ne(server.SignUp("h8liu"))
	er(server.SignUp(" h8liu"))
	er(server.SignUp("8hliu"))
	er(server.SignUp("H8liu"))

	ne(server.SignUp("fenglu"))

	users, e := server.ListUsers()
	ne(e)

	as(len(users) == 2)
	sort.Strings(users)
	as(users[0] == "fenglu")
	as(users[1] == "h8liu")

	ne(server.Follow("h8liu", "fenglu"))
	b, e := server.IsFollowing("h8liu", "fenglu")
	ne(e)
	as(b)

	b, e = server.IsFollowing("fenglu", "h8liu")
	ne(e)
	as(!b)

	b, e = server.IsFollowing("h8liu", "fenglu2")
	er(e)
	as(!b)

	ne(server.Unfollow("h8liu", "fenglu"))
	er(server.Unfollow("h8liu", "fenglu"))

	b, e = server.IsFollowing("h8liu", "fenglu")
	ne(e)
	as(!b)

	ne(server.Follow("h8liu", "fenglu"))

	clk := uint64(0)

	er(server.Post("", "", clk))

	longMsg := ""
	for i := 0; i < 200; i++ {
		longMsg += " "
	}

	er(server.Post("h8liu", longMsg, clk))
	ne(server.Post("h8liu", "hello, world", clk))

	clk = uint64(0)

	tribs, e := server.Tribs("h8liu")
	ne(e)
	as(len(tribs) == 1)
	tr := tribs[0]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world")
	if tr.Clock > clk {
		clk = tr.Clock
	}

	tribs, e = server.Home("fenglu")
	ne(e)
	as(tribs != nil)
	as(len(tribs) == 0)

	ne(server.Follow("fenglu", "h8liu"))
	tribs, e = server.Home("fenglu")
	ne(e)
	as(len(tribs) == 1)
	tr = tribs[0]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world")
	if tr.Clock > clk {
		clk = tr.Clock
	}

	ne(server.Post("h8liu", "hello, world2", clk))
	tribs, e = server.Home("fenglu")
	ne(e)
	as(len(tribs) == 2)
	tr = tribs[0]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world")

	tr = tribs[1]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world2")

	er(server.Follow("fenglu", "fenglu"))
	er(server.Follow("fengl", "fenglu"))
	er(server.Follow("fenglu", "fengl"))
	er(server.Follow("fenglu", "h8liu"))

	tribs, e = server.Home("h8liu")
	ne(e)
	as(len(tribs) == 2)
	tr = tribs[0]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world")

	tr = tribs[1]
	as(tr.User == "h8liu")
	as(tr.Message == "hello, world2")

	ne(server.SignUp("rkapoor"))
	fos, e := server.Following("rkapoor")
	ne(e)
	as(fos != nil)
	as(len(fos) == 0)
}