From af53d3fe495fb36433725fddc01cea0103de50b7 Mon Sep 17 00:00:00 2001 From: Tobias Pfandzelter Date: Mon, 26 Jul 2021 15:27:47 +0200 Subject: [PATCH] add sorting to vector clock String encoding --- govec/vclock/vclock.go | 3 +++ govec/vclock/vclock_test.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/govec/vclock/vclock.go b/govec/vclock/vclock.go index c8f2948..961bfcc 100644 --- a/govec/vclock/vclock.go +++ b/govec/vclock/vclock.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "fmt" "log" + "sort" ) // Condition constants define how to compare a vector clock against another, @@ -121,6 +122,8 @@ func (vc VClock) ReturnVCString() string { i++ } + sort.Strings(ids) + var buffer bytes.Buffer buffer.WriteString("{") for i := range ids { diff --git a/govec/vclock/vclock_test.go b/govec/vclock/vclock_test.go index d8f1a32..48c0a8f 100644 --- a/govec/vclock/vclock_test.go +++ b/govec/vclock/vclock_test.go @@ -301,3 +301,23 @@ func TestEncodeDecode(t *testing.T) { t.Fatalf("decoded not the same as encoded enc = %s | dec = %s", nString, dString) } } + +func TestVCString(t *testing.T) { + n := New() + + n.Set("a", 1) + n.Set("b", 1) + n.Set("c", 1) + n.Set("d", 1) + n.Set("e", 1) + n.Set("f", 1) + n.Set("g", 1) + n.Set("h", 1) + + expected := "{\"a\":1, \"b\":1, \"c\":1, \"d\":1, \"e\":1, \"f\":1, \"g\":1, \"h\":1}" + nString := n.ReturnVCString() + + if nString != expected { + t.Fatalf("VC string %s not the same as expected %s", nString, expected ) + } +} \ No newline at end of file