Skip to content

Commit

Permalink
✅ [channel/test,backend/eth/channel] Generic subscription test
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs committed Sep 23, 2021
1 parent 92731c2 commit 1513db6
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
112 changes: 112 additions & 0 deletions backend/ethereum/channel/subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2021 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package channel_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"perun.network/go-perun/backend/ethereum/channel/test"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "perun.network/go-perun/pkg/test"
)

func TestSubscription(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

rng := pkgtest.Prng(t)
numParts := 2 + rng.Intn(maxNumParts-2)
setup := test.NewSetup(t, rng, numParts, blockInterval)
adjSetup := newAdjudicatorSetup(setup)
adj := adjSetup.Adjudicator()

req, subChannels := adjSetup.newAdjudicatorReq(ctx, rng, channeltest.WithoutApp()), []channel.SignedState{}
sub, err := adj.Subscribe(ctx, req.Params.ID())
require.NoError(t, err, "subscribing")

subSetup := newSubscriptionSetup(sub, adj, req, subChannels)
channeltest.TestSubscription(ctx, t, subSetup)
}

type subscriptionSetup struct {
adj channel.Adjudicator
sub *testSubscription
req *channel.AdjudicatorReq
subChannels []channel.SignedState
}

func newSubscriptionSetup(
sub channel.AdjudicatorSubscription,
adj channel.Adjudicator,
req *channel.AdjudicatorReq,
subChannels []channel.SignedState,
) *subscriptionSetup {
return &subscriptionSetup{
sub: &testSubscription{
emitProgressed: false,
req: req,
AdjudicatorSubscription: sub,
},
adj: adj,
req: req,
subChannels: subChannels,
}
}

type testSubscription struct {
channel.AdjudicatorSubscription
emitProgressed bool
req *channel.AdjudicatorReq
}

// Next emulates app channel functionality because app channels are not supported yet.
func (s *testSubscription) Next() channel.AdjudicatorEvent {
if s.emitProgressed {
s.emitProgressed = false
return channel.NewProgressedEvent(s.req.Tx.ID, &channel.ElapsedTimeout{}, s.req.Tx.State, s.req.Idx)
}
return s.AdjudicatorSubscription.Next()
}

func (s *subscriptionSetup) Subscription() channel.AdjudicatorSubscription {
return s.sub
}

// EmitRegistered operates the adjudicator so that a registered event should be emitted.
func (s *subscriptionSetup) EmitRegistered(ctx context.Context) (channel.Params, channel.State) {
err := s.adj.Register(ctx, *s.req, s.subChannels)
if err != nil {
panic(err)
}
return *s.req.Params, *s.req.Tx.State
}

// EmitProgressed emulates app channel functionality because app channels are not supported yet.
func (s *subscriptionSetup) EmitProgressed(ctx context.Context) (channel.Params, channel.State) {
s.sub.emitProgressed = true
return *s.req.Params, *s.req.Tx.State
}

// EmitRegistered operates the adjudicator so that a concluded event should be emitted.
func (s *subscriptionSetup) EmitConcluded(ctx context.Context) channel.Params {
err := s.adj.Withdraw(ctx, *s.req, channeltest.MakeStateMapFromSignedStates(s.subChannels...))
if err != nil {
panic(err)
}
return *s.req.Params
}
76 changes: 76 additions & 0 deletions channel/test/subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2021 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"perun.network/go-perun/channel"
)

// SubscriptionTestSetup represents a setup for testing a subscription implementation.
//
// Subscription should return an instance of the subscription implementation.
// EmitRegistered should make the subscription emit a registered event.
// EmitProgressed should make the subscription emit a progressed event.
// EmitConcluded should make the subscription emit a concluded event.
type SubscriptionTestSetup interface {
Subscription() channel.AdjudicatorSubscription
EmitRegistered(context.Context) (channel.Params, channel.State)
EmitProgressed(context.Context) (channel.Params, channel.State)
EmitConcluded(context.Context) channel.Params
}

func TestSubscription(ctx context.Context, t *testing.T, s SubscriptionTestSetup) {
sub := s.Subscription()

{
params, state := s.EmitRegistered(ctx)
e, ok := sub.Next().(*channel.RegisteredEvent)
assert.True(t, ok, "registered")
assert.True(t, e.ID() == params.ID(), "equal ID")
assert.True(t, e.State.Equal(&state) == nil, "equal state")
err := e.Timeout().Wait(ctx)
assert.NoError(t, err, "registered: waiting")
}

{
params, state := s.EmitProgressed(ctx)
e, ok := sub.Next().(*channel.ProgressedEvent)
assert.True(t, ok, "progressed")
assert.True(t, e.ID() == params.ID(), "equal ID")
assert.True(t, e.State.Equal(&state) == nil, "equal state")
err := e.Timeout().Wait(ctx)
assert.NoError(t, err, "progressed: waiting")
}

{
params := s.EmitConcluded(ctx)
e, ok := sub.Next().(*channel.ConcludedEvent)
assert.True(t, ok, "concluded")
assert.True(t, e.ID() == params.ID(), "equal ID")
err := e.Timeout().Wait(ctx)
assert.NoError(t, err, "concluded: waiting")
}

{
err := sub.Close()
assert.NoError(t, err, "close")
err = sub.Err()
assert.NoError(t, err, "err")
}
}

0 comments on commit 1513db6

Please sign in to comment.