Example #1
0
func (S) TestMerge(c *C) {
	vc1 := vclock.New()
	vc2 := vclock.New()
	vc1.Update("idA", 0)
	vc1.Update("idA", 0) // counter > than vc2
	vc1.Update("idB", 0) // counter < than vc2
	vc1.Update("idC", 0) // counter not in vc2
	vc2.Update("idA", 0)
	vc2.Update("idB", 0)
	vc2.Update("idB", 0)
	vc2.Update("idD", 0) // counter not in vc1

	vc2.Merge(vc1)

	vc1.Update("idB", 0)
	vc1.Update("idD", 0)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, true)
}
Example #2
0
func (s *S) TestTruncate(c *C) {
	for testN, test := range truncTests {
		before := vclock.New()
		after := vclock.New()
		for i := 0; i != len(test.before); i++ {
			for j := 0; j != test.before[i].updates; j++ {
				before.Update(test.before[i].id, test.before[i].time)
			}
		}
		for i := 0; i != len(test.after); i++ {
			for j := 0; j != test.after[i].updates; j++ {
				after.Update(test.after[i].id, test.after[i].time)
			}
		}
		truncated := before.Truncate(&test.trunc)
		cmt := Commentf("Truncation test %d failed: %s: %#v", testN, test.summary, truncated.Bytes())
		c.Assert(truncated.Compare(after, vclock.Equal), Equals, true, cmt)
	}
}
Example #3
0
func (s *S) TestJson(c *C) {
	vc1 := vclock.New()
	vc1.Update("a", 1)

	j1, _ := json.Marshal(vc1)

	var vc2 *vclock.VClock
	_ = json.Unmarshal(j1, &vc2)

	c.Assert(vc1, DeepEquals, vc2)
}
Example #4
0
func (S) TestCopy(c *C) {
	vc1 := vclock.New()
	vc1.Update("idA", 0)
	vc2 := vc1.Copy()
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, true)
	vc2.Update("idA", 0)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, false)
	vc1.Update("idA", 0)
	vc2.Update("idB", 0)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, false)
}
Example #5
0
func (S) TestBytesWithTime(c *C) {
	// When there's one or more values with time, the format becomes:
	// [ header byte | [ ticks | time | id len | id ] * N ]
	vc := vclock.New()
	vc.Update("idA", 15)
	c.Assert(vc.Bytes(), DeepEquals, []byte{1, 1, 15, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)
	vc.Update("idB", 5)
	vc.Update("idB", 255)
	c.Assert(vc.Bytes(), DeepEquals, []byte{1, 1, 15, 3, 'i', 'd', 'A', 2, 129, 127, 3, 'i', 'd', 'B'})
	testFromBytes(c, vc)
}
Example #6
0
func (S) TestUpdateAndCompare(c *C) {
	vc1 := vclock.New()
	vc2 := vclock.New()
	c.Assert(vc1.Compare(vc2, vclock.Equal), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Equal), Equals, false)
	vc2.Update("idA", 0)
	c.Assert(vc1.Compare(vc2, vclock.Descendant), Equals, true)
	c.Assert(vc1.Compare(vc2, vclock.Equal), Equals, false)
	c.Assert(vc1.Compare(vc2, ^vclock.Descendant), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Ancestor), Equals, true)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, false)
	c.Assert(vc2.Compare(vc1, ^vclock.Ancestor), Equals, false)
	vc1.Update("idA", 0)
	c.Assert(vc1.Compare(vc2, vclock.Equal), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Equal), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, true)
	c.Assert(vc2.Compare(vc1, ^vclock.Equal), Equals, false)
	vc1.Update("idB", 0)
	c.Assert(vc1.Compare(vc2, vclock.Ancestor), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Ancestor), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Descendant), Equals, true)
	c.Assert(vc2.Compare(vc1, ^vclock.Descendant), Equals, false)
	vc2.Update("idA", 0)
	c.Assert(vc1.Compare(vc2, vclock.Concurrent), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Concurrent), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Concurrent), Equals, true)
	c.Assert(vc2.Compare(vc1, ^vclock.Concurrent), Equals, false)
	vc2.Update("idB", 0)
	c.Assert(vc1.Compare(vc2, vclock.Descendant), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Descendant), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Ancestor), Equals, true)
	c.Assert(vc2.Compare(vc1, ^vclock.Ancestor), Equals, false)
	vc2.Update("idB", 0)
	c.Assert(vc1.Compare(vc2, vclock.Descendant), Equals, true)
	c.Assert(vc1.Compare(vc2, ^vclock.Descendant), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Ancestor), Equals, true)
	c.Assert(vc2.Compare(vc1, ^vclock.Ancestor), Equals, false)
}
Example #7
0
func (S) TestLastUpdate(c *C) {
	vc := vclock.New()
	c.Assert(vc.LastUpdate(), Equals, uint64(0))
	vc.Update("idA", 5)
	c.Assert(vc.LastUpdate(), Equals, uint64(5))
	vc.Update("idB", 3)
	c.Assert(vc.LastUpdate(), Equals, uint64(5))
	vc.Update("idC", 7)
	c.Assert(vc.LastUpdate(), Equals, uint64(7))
	vc.Update("idB", 9)
	c.Assert(vc.LastUpdate(), Equals, uint64(9))
	vc.Update("idB", 7)
	c.Assert(vc.LastUpdate(), Equals, uint64(9))
}
Example #8
0
func (S) TestBytesSimple(c *C) {
	// The basic format of the bytes representation is:
	// [ header byte | [ ticks | id len | id ] * N ]
	vc := vclock.New()
	c.Assert(len(vc.Bytes()), Equals, 0)
	testFromBytes(c, vc)
	vc.Update("idA", 0)
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 1, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)
	vc.Update("idB", 0)
	vc.Update("idB", 0)
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 1, 3, 'i', 'd', 'A', 2, 3, 'i', 'd', 'B'})
	testFromBytes(c, vc)
}
Example #9
0
func (S) TestCompareWithMissingInOther(c *C) {
	vc1 := vclock.New()
	vc1.Update("idA", 1)
	vc2 := vc1.Copy()
	vc2.Update("idB", 1)
	vc1.Update("idC", 5)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Ancestor), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Descendant), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Concurrent), Equals, true)
	vc1.Update("idD", 5)
	c.Assert(vc2.Compare(vc1, vclock.Equal), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Ancestor), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Descendant), Equals, false)
	c.Assert(vc2.Compare(vc1, vclock.Concurrent), Equals, true)
}
Example #10
0
func (S) TestBytesIdLenPacking(c *C) {
	vc := vclock.New()

	id := make([]byte, 255)
	for i := 0; i != len(id); i++ {
		id[i] = 'X'
	}

	vc.Update(string(id), 0)

	result := vc.Bytes()

	// len(id) = 255
	// 129 = 1 0000001 - Continuation bit on + 8th bit.
	// 127 = 0 1111111 - Lower 7 bits.
	c.Assert(result[:4], DeepEquals, []byte{0, 1, 129, 127})
	c.Assert(result[4:], DeepEquals, id)
	testFromBytes(c, vc)
}
Example #11
0
func (S) TestBytesTicksPacking(c *C) {
	vc := vclock.New()
	for i := 0; i != 127; i++ {
		vc.Update("idA", 0)
	}

	// Ticks = 127
	// 127 = 0 1111111 - Continuation bit off.
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 127, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)

	vc.Update("idA", 0)

	// Ticks = 128
	// 129 = 1 0000001 - Continuation bit on + 8th bit.
	//   0 = 0 0000000 - Lower 7 bits.
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 129, 0, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)

	for i := 0; i != 127; i++ {
		vc.Update("idA", 0)
	}

	// Ticks = 255
	// 129 = 1 0000001 - Continuation bit on + 8th bit.
	// 127 = 0 1111111 - Lower 7 bits.
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 129, 127, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)

	vc.Update("idA", 0)

	// Ticks = 256
	// 129 = 1 0000010 - Continuation bit on + 9th bit.
	//   0 = 0 0000000 - Lower 7 bits.
	c.Assert(vc.Bytes(), DeepEquals, []byte{0, 130, 0, 3, 'i', 'd', 'A'})
	testFromBytes(c, vc)
}