Skip to content

Commit

Permalink
fix(internal_tests): fix failing unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Minh Huy Tran <[email protected]>
  • Loading branch information
NhoxxKienn authored and sophia1ch committed Feb 20, 2024
1 parent 40f6013 commit e08ee0b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ PolyCrypt GmbH
Oliver Tale-Yazdi <[email protected]>
Ilja von Hoessle <[email protected]>
Jens Winkle <[email protected]>
Minh Huy Tran <[email protected]>

Robert Bosch GmbH
Manoranjith <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion channel/test/app_randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions channel/test/app_randomizer_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 14 additions & 1 deletion channel/test/mock_app_randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 12 additions & 1 deletion wire/net/simple/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit e08ee0b

Please sign in to comment.