lotus/paychmgr/msglistener_test.go

97 lines
1.7 KiB
Go
Raw Normal View History

2020-07-28 23:16:47 +00:00
package paychmgr
import (
"testing"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
)
func testCids() []cid.Cid {
c1, _ := cid.Decode("QmdmGQmRgRjazArukTbsXuuxmSHsMCcRYPAZoGhd6e3MuS")
c2, _ := cid.Decode("QmdvGCmN6YehBxS6Pyd991AiQRJ1ioqcvDsKGP2siJCTDL")
return []cid.Cid{c1, c2}
}
func TestMsgListener(t *testing.T) {
ml := newMsgListeners()
2020-07-28 23:16:47 +00:00
done := false
experr := xerrors.Errorf("some err")
cids := testCids()
ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
require.Equal(t, experr, err)
done = true
})
ml.fireMsgComplete(cids[0], experr)
if !done {
t.Fatal("failed to fire event")
}
}
func TestMsgListenerNilErr(t *testing.T) {
ml := newMsgListeners()
2020-07-28 23:16:47 +00:00
done := false
cids := testCids()
ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
require.Nil(t, err)
done = true
})
ml.fireMsgComplete(cids[0], nil)
if !done {
t.Fatal("failed to fire event")
}
}
func TestMsgListenerUnsub(t *testing.T) {
ml := newMsgListeners()
2020-07-28 23:16:47 +00:00
done := false
experr := xerrors.Errorf("some err")
cids := testCids()
unsub := ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
t.Fatal("should not call unsubscribed listener")
})
ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
require.Equal(t, experr, err)
done = true
})
unsub()
2020-07-28 23:16:47 +00:00
ml.fireMsgComplete(cids[0], experr)
if !done {
t.Fatal("failed to fire event")
}
}
func TestMsgListenerMulti(t *testing.T) {
ml := newMsgListeners()
2020-07-28 23:16:47 +00:00
count := 0
cids := testCids()
ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
count++
})
ml.onMsgComplete(cids[0], func(err error) {
2020-07-28 23:16:47 +00:00
count++
})
ml.onMsgComplete(cids[1], func(err error) {
2020-07-28 23:16:47 +00:00
count++
})
ml.fireMsgComplete(cids[0], nil)
require.Equal(t, 2, count)
ml.fireMsgComplete(cids[1], nil)
require.Equal(t, 3, count)
}