From e08ee0bfe1474f46ba2cc87a6f50b8fefb68571d Mon Sep 17 00:00:00 2001 From: Minh Huy Tran Date: Tue, 6 Feb 2024 12:51:59 +0100 Subject: [PATCH] fix(internal_tests): fix failing unit tests Signed-off-by: Minh Huy Tran --- MAINTAINERS.md | 1 + NOTICE | 1 + channel/test/app_randomizer.go | 2 +- channel/test/app_randomizer_internal_test.go | 4 ++-- channel/test/mock_app_randomizer.go | 15 ++++++++++++++- go.mod | 1 + go.sum | 2 ++ wire/net/simple/init.go | 13 ++++++++++++- 8 files changed, 34 insertions(+), 5 deletions(-) diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 818dd7af7..6a026a2fa 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -9,6 +9,7 @@ | Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt | | Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh | | Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 | +| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn | ## Emeritus Maintainers diff --git a/NOTICE b/NOTICE index 371455648..1476ec69c 100644 --- a/NOTICE +++ b/NOTICE @@ -42,6 +42,7 @@ PolyCrypt GmbH Oliver Tale-Yazdi Ilja von Hoessle Jens Winkle + Minh Huy Tran Robert Bosch GmbH Manoranjith diff --git a/channel/test/app_randomizer.go b/channel/test/app_randomizer.go index 3cc69a1cf..ae7442b20 100644 --- a/channel/test/app_randomizer.go +++ b/channel/test/app_randomizer.go @@ -27,7 +27,7 @@ type AppRandomizer interface { NewRandomData(*rand.Rand) channel.Data } -var appRandomizer AppRandomizer = &MockAppRandomizer{} +var appRandomizer AppRandomizer = NewMockAppRandomizer() // isAppRandomizerSet tracks whether the AppRandomizer was already set // with `SetAppRandomizer`. diff --git a/channel/test/app_randomizer_internal_test.go b/channel/test/app_randomizer_internal_test.go index 64d7b9bb8..3416841a2 100644 --- a/channel/test/app_randomizer_internal_test.go +++ b/channel/test/app_randomizer_internal_test.go @@ -29,13 +29,13 @@ func TestAppRandomizerSet(t *testing.T) { assert.False(t, isAppRandomizerSet, "isAppRandomizerSet should be defaulted to false") old := appRandomizer - assert.NotPanics(t, func() { SetAppRandomizer(&MockAppRandomizer{}) }, "first SetAppRandomizer() should work") + assert.NotPanics(t, func() { SetAppRandomizer(NewMockAppRandomizer()) }, "first SetAppRandomizer() should work") assert.True(t, isAppRandomizerSet, "isAppRandomizerSet should be true") assert.NotNil(t, appRandomizer, "appRandomizer should not be nil") assert.False(t, old == appRandomizer, "appRandomizer should have changed") old = appRandomizer - assert.Panics(t, func() { SetAppRandomizer(&MockAppRandomizer{}) }, "second SetAppRandomizer() should panic") + assert.Panics(t, func() { SetAppRandomizer(NewMockAppRandomizer()) }, "second SetAppRandomizer() should panic") assert.True(t, isAppRandomizerSet, "isAppRandomizerSet should be true") assert.NotNil(t, appRandomizer, "appRandomizer should not be nil") assert.True(t, old == appRandomizer, "appRandomizer should not have changed") diff --git a/channel/test/mock_app_randomizer.go b/channel/test/mock_app_randomizer.go index a03a9cff0..0e20c2493 100644 --- a/channel/test/mock_app_randomizer.go +++ b/channel/test/mock_app_randomizer.go @@ -17,11 +17,19 @@ package test import ( "math/rand" + "github.com/google/uuid" "perun.network/go-perun/channel" ) // MockAppRandomizer implements the AppRandomizer interface. -type MockAppRandomizer struct{} +type MockAppRandomizer struct { + id uuid.UUID // Unique identifier for each instance +} + +// NewMockAppRandomizer creates a new instance of MockAppRandomizer with a unique identifier. +func NewMockAppRandomizer() *MockAppRandomizer { + return &MockAppRandomizer{id: uuid.New()} +} // NewRandomApp creates a new MockApp with a random address. func (MockAppRandomizer) NewRandomApp(rng *rand.Rand) channel.App { @@ -32,3 +40,8 @@ func (MockAppRandomizer) NewRandomApp(rng *rand.Rand) channel.App { func (MockAppRandomizer) NewRandomData(rng *rand.Rand) channel.Data { return channel.NewMockOp(channel.MockOp(rng.Uint64())) } + +// Equal returns false for any comparison, ensuring two MockAppRandomizers are always considered different. +func (m *MockAppRandomizer) Equal(other *MockAppRandomizer) bool { + return m.id != other.id +} diff --git a/go.mod b/go.mod index 5d66b4575..a765acf82 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/go-cmp v0.5.4 // indirect + github.com/google/uuid v1.6.0 github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect diff --git a/go.sum b/go.sum index 3e51d0b9f..80c0f338c 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= diff --git a/wire/net/simple/init.go b/wire/net/simple/init.go index 29af22ab2..cce04c416 100644 --- a/wire/net/simple/init.go +++ b/wire/net/simple/init.go @@ -14,10 +14,21 @@ package simple -import "perun.network/go-perun/wire" +import ( + "math/rand" + + "perun.network/go-perun/wire" + "perun.network/go-perun/wire/test" +) func init() { wire.SetNewAddressFunc(func() wire.Address { return NewAddress("") }) + test.SetNewRandomAddress(func(rng *rand.Rand) wire.Address { + return NewRandomAddress(rng) + }) + test.SetNewRandomAccount(func(rng *rand.Rand) wire.Account { + return NewRandomAccount(rng) + }) }