Пример #1
0
// TestEngineMerge tests that the passing through of engine merge operations
// to the goMerge function works as expected. The semantics are tested more
// exhaustively in the merge tests themselves.
func TestEngineMerge(t *testing.T) {
	runWithAllEngines(func(engine Engine, t *testing.T) {
		testKey := Key("haste not in life")
		merges := [][]byte{
			[]byte(encoding.MustGobEncode(Appender("x"))),
			[]byte(encoding.MustGobEncode(Appender("y"))),
			[]byte(encoding.MustGobEncode(Appender("z"))),
		}
		for i, update := range merges {
			if err := engine.merge(testKey, update); err != nil {
				t.Fatalf("%d: %v", i, err)
			}
		}
		result, _ := engine.get(testKey)
		if !bytes.Equal(encoding.MustGobDecode(result).(Appender), Appender("xyz")) {
			t.Errorf("unexpected append-merge result")
		}
	}, t)
}
Пример #2
0
// TestGoMerge tests the function goMerge but not the integration with
// the storage engines. For that, see the engine tests.
func TestGoMerge(t *testing.T) {
	// Let's start with stuff that should go wrong.
	badCombinations := []struct {
		old, update interface{}
	}{
		{Counter(0), Appender("")},
		{[]byte("apple"), nil},
		{"", ""},
		{0, 0},
		{Appender(""), Counter(0)},
		{0, "asd"},
		{float64(1.3), Counter(0)},
		{Counter(0), nil},
	}
	for i, c := range badCombinations {
		_, err := goMerge(encoding.MustGobEncode(c.old), encoding.MustGobEncode(c.update))
		if err == nil {
			t.Errorf("goMerge: %d: expected error", i)
		}
	}

	testCasesCounter := []struct {
		old, update, expected Counter
		wantError             bool
	}{
		{0, 10, 10, false},
		{10, 20, 30, false},
		{595, -600, -5, false},
		// Close to overflow, but not quite there.
		{math.MinInt64 + 3, -3, math.MinInt64, false},
		{math.MaxInt64, 0, math.MaxInt64, false},
		// Overflows.
		{math.MaxInt64, 1, 0, true},
		{-1, math.MinInt64, 0, true},
	}

	gibber1, gibber2 := gibberishBytes(100), gibberishBytes(200)

	testCasesAppender := []struct {
		old, update, expected Appender
	}{
		{Appender(""), Appender(""), Appender("")},
		{nil, Appender(""), Appender("")},
		{nil, nil, nil},
		{Appender("\n "), Appender(" \t "), Appender("\n  \t ")},
		{Appender("ქართული"), Appender("\nKhartuli"), Appender("ქართული\nKhartuli")},
		{gibber1, gibber2, append(append([]byte(nil), gibber1...), gibber2...)},
	}

	for i, c := range testCasesCounter {
		oEncoded := encoding.MustGobEncode(c.old)
		uEncoded := encoding.MustGobEncode(c.update)

		result, err := goMerge(oEncoded, uEncoded)
		if c.wantError {
			if err == nil {
				t.Errorf("goMerge: %d: wanted error but got success", i)
			}
			continue
		}
		if err != nil {
			t.Errorf("goMerge error: %d: %v", i, err)
			continue
		}
		resultDecoded := encoding.MustGobDecode(result)
		if resultDecoded != c.expected {
			t.Errorf("goMerge error: %d: want %v, get %v", i, c.expected, result)
		}
	}
	for i, c := range testCasesAppender {
		oEncoded := encoding.MustGobEncode(c.old)
		uEncoded := encoding.MustGobEncode(c.update)

		result, err := goMerge(oEncoded, uEncoded)
		if err != nil {
			t.Errorf("goMerge error: %d: %v", i, err)
			continue
		}
		resultDecoded := encoding.MustGobDecode(result)
		if !bytes.Equal(resultDecoded.(Appender), c.expected) {
			t.Errorf("goMerge error: %d: want %v, get %v", i, c.expected, result)
		}
	}

}