Beispiel #1
0
func TestUpdatePosts(t *testing.T) {
	mon := &Monitor{
		op: operator.NewMock(`{
			"data": {
				"children": [
					{"data":{"name":"4"}},
					{"data":{"name":"3"}},
					{"data":{"name":"2"}},
					{"data":{"name":"1"}}
				]
			}
		}`),
		NewPosts: make(chan *redditproto.Link),
		tip:      list.New(),
	}
	mon.tip.PushFront("")

	go func() {
		for true {
			<-mon.NewPosts
		}
	}()

	count, err := mon.updatePosts()
	if err != nil {
		t.Fatal("error: %v", err)
	}

	if count != 4 {
		t.Errorf("got %d posts, wanted 4 posts", count)
	}
}
Beispiel #2
0
func TestFixTip(t *testing.T) {
	mon := &Monitor{
		op: operator.NewMock(`{
			"data": {
				"children": [
					{"data":{"name":"1"}},
					{"data":{"name":"2"}},
					{"data":{"name":"3"}},
					{"data":{"name":"4"}}
				]
			}
		}`),
		tip: list.New(),
	}
	mon.tip.PushFront("1")
	mon.tip.PushFront("0")
	broken, err := mon.fixTip()
	if err != nil {
		t.Fatalf("error: %v", err)
	}
	if mon.tip.Front().Value.(string) != "1" {
		t.Errorf(
			"wanted '1'; got %s",
			mon.tip.Front().Value.(string))
	}
	if !broken {
		t.Errorf("wanted fixTip to indicate broken; 0 was gone")
	}

	mon.tip = list.New()
	mon.tip.PushFront("mark")
	mon.tip.PushFront("internet")
	_, err = mon.fixTip()
	if err != nil {
		t.Fatalf("error: %v", err)
	}
	if mon.tip.Front().Value.(string) != "" {
		t.Errorf(
			"wanted ''; got %s",
			mon.tip.Front().Value.(string))
	}

	mon.tip = list.New()
	mon.tip.PushFront("1")
	broken, err = mon.fixTip()
	if err != nil {
		t.Fatalf("error: %v", err)
	}
	if broken {
		t.Errorf("wanted broken to be false; '1' was valid tip")
	}
}
Beispiel #3
0
func TestCheckOnTip(t *testing.T) {
	mon := &Monitor{
		op: operator.NewMock(`{
			"data": {
				"children": [
					{"data":{"name":"4"}},
					{"data":{"name":"3"}},
					{"data":{"name":"2"}},
					{"data":{"name":"1"}}
				]
			}
		}`),
		tip: list.New(),
	}
	mon.tip.PushFront("4")

	if err := mon.checkOnTip(1); err != nil {
		t.Fatalf("error: %v\n", err)
	}
	if mon.blanks != 0 {
		t.Errorf("unwanted blank count increment; got %d", mon.blanks)
	}

	oldTolerance := mon.blankRoundTolerance
	mon.blanks = mon.blankRoundTolerance
	if err := mon.checkOnTip(0); err != nil {
		t.Fatalf("error: %v\n", err)
	}
	if mon.blanks != 0 {
		t.Errorf("got blank round count %d; wanted 0", mon.blanks)
	}
	if mon.blankRoundTolerance <= oldTolerance {
		t.Errorf(
			"got %d tolerance; wanted > %d",
			mon.blankRoundTolerance,
			oldTolerance)
	}
}
Beispiel #4
0
func TestTip(t *testing.T) {
	mon := &Monitor{
		op: operator.NewMock(`{
			"data": {
				"children": [
					{"data":{"name":"4"}},
					{"data":{"name":"3"}},
					{"data":{"name":"2"}},
					{"data":{"name":"1"}}
				]
			}
		}`),
		tip: list.New(),
	}
	mon.tip.PushFront("shouldnotbeatback")
	for i := 0; i < maxTipSize-1; i++ {
		mon.tip.PushFront("bunk")
	}

	posts, err := mon.fetchTip()
	if err != nil {
		t.Fatalf("error: %v", err)
	}

	if mon.tip.Back().Value.(string) == "shouldnotbeatback" {
		t.Errorf("tips were not truncated at capacity")
	}

	if mon.tip.Front().Value.(string) != "4" {
		t.Errorf(
			"wanted front tip '4'; got %s",
			mon.tip.Front().Value.(string))
	}

	if len(posts) != 4 {
		t.Errorf("wanted 4 incrementally named posts; got %v", posts)
	}
}