From cb9092eb8f76e10c2e1b7874b0bf91e4e460072f Mon Sep 17 00:00:00 2001 From: "tung.tq" Date: Thu, 30 Nov 2023 09:10:23 +0700 Subject: [PATCH] Test Consistency with Two Nodes --- .github/workflows/go.yml | 6 ++- item/item_property_test.go | 81 +++++++++++++++++++++++++++++++++++--- 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a6ad0a0..93fc470 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -9,9 +9,13 @@ jobs: runs-on: ubuntu-20.04 services: memcached: - image: memcached:1.6.18 + image: memcached:1.6.19 ports: - 11211:11211 + memcached2: + image: memcached:1.6.19 + ports: + - 11212:11211 steps: - uses: actions/checkout@v2 - uses: actions/setup-go@v2 diff --git a/item/item_property_test.go b/item/item_property_test.go index 5a41ef9..f101488 100644 --- a/item/item_property_test.go +++ b/item/item_property_test.go @@ -3,19 +3,23 @@ package item import ( "context" "fmt" - "github.com/QuangTung97/go-memcache/memcache" - "github.com/QuangTung97/memproxy" - "github.com/QuangTung97/memproxy/proxy" - "github.com/stretchr/testify/assert" "math/rand" "sync" "testing" "time" + + "github.com/QuangTung97/go-memcache/memcache" + "github.com/stretchr/testify/assert" + + "github.com/QuangTung97/memproxy" + "github.com/QuangTung97/memproxy/proxy" ) type itemPropertyTest struct { - client *memcache.Client - mc memproxy.Memcache + client *memcache.Client + client2 *memcache.Client + + mc memproxy.Memcache mut sync.Mutex currentAge int64 @@ -62,6 +66,14 @@ func (p *itemPropertyTest) flushAll() { if err != nil { panic(err) } + + if p.client2 != nil { + pipe := p.client2.Pipeline() + err := pipe.FlushAll()() + if err != nil { + panic(err) + } + } } func newItemPropertyTest(t *testing.T) *itemPropertyTest { @@ -107,6 +119,50 @@ func newItemPropertyTestWithProxy(t *testing.T) *itemPropertyTest { return p } +func newItemPropertyTestWithProxyTwoNodes(t *testing.T) *itemPropertyTest { + p := &itemPropertyTest{} + + client1, err := memcache.New("localhost:11211", 3) + if err != nil { + panic(err) + } + + client2, err := memcache.New("localhost:11212", 3) + if err != nil { + panic(err) + } + + t.Cleanup(func() { + _ = client1.Close() + _ = client2.Close() + }) + + p.client = client1 + p.client2 = client2 + + servers := []proxy.SimpleServerConfig{ + { + Host: "localhost", + Port: 11211, + }, + { + Host: "localhost", + Port: 11212, + }, + } + mc, closeFunc, err := proxy.NewSimpleReplicatedMemcache( + servers, 3, + proxy.NewSimpleStats(servers), + ) + if err != nil { + panic(err) + } + t.Cleanup(closeFunc) + p.mc = mc + + return p +} + func (p *itemPropertyTest) testConsistency(t *testing.T) { var wg sync.WaitGroup @@ -193,4 +249,17 @@ func TestProperty_SingleKey(t *testing.T) { p.testConsistency(t) } }) + + t.Run("with-proxy-two-nodes", func(t *testing.T) { + seed := time.Now().UnixNano() + rand.Seed(seed) + fmt.Println("SEED:", seed) + + p := newItemPropertyTestWithProxy(t) + + for i := 0; i < 100; i++ { + p.flushAll() + p.testConsistency(t) + } + }) }