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) {
|
2020-08-10 21:52:59 +00:00
|
|
|
ml := newMsgListeners()
|
2020-07-28 23:16:47 +00:00
|
|
|
|
|
|
|
done := false
|
|
|
|
experr := xerrors.Errorf("some err")
|
|
|
|
cids := testCids()
|
2020-08-10 21:52:59 +00:00
|
|
|
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) {
|
2020-08-10 21:52:59 +00:00
|
|
|
ml := newMsgListeners()
|
2020-07-28 23:16:47 +00:00
|
|
|
|
|
|
|
done := false
|
|
|
|
cids := testCids()
|
2020-08-10 21:52:59 +00:00
|
|
|
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) {
|
2020-08-10 21:52:59 +00:00
|
|
|
ml := newMsgListeners()
|
2020-07-28 23:16:47 +00:00
|
|
|
|
|
|
|
done := false
|
|
|
|
experr := xerrors.Errorf("some err")
|
|
|
|
cids := testCids()
|
2020-08-10 21:52:59 +00:00
|
|
|
unsub := ml.onMsgComplete(cids[0], func(err error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
t.Fatal("should not call unsubscribed listener")
|
|
|
|
})
|
2020-08-10 21:52:59 +00:00
|
|
|
ml.onMsgComplete(cids[0], func(err error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
require.Equal(t, experr, err)
|
|
|
|
done = true
|
|
|
|
})
|
|
|
|
|
2020-08-10 21:52:59 +00:00
|
|
|
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) {
|
2020-08-10 21:52:59 +00:00
|
|
|
ml := newMsgListeners()
|
2020-07-28 23:16:47 +00:00
|
|
|
|
|
|
|
count := 0
|
|
|
|
cids := testCids()
|
2020-08-10 21:52:59 +00:00
|
|
|
ml.onMsgComplete(cids[0], func(err error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
count++
|
|
|
|
})
|
2020-08-10 21:52:59 +00:00
|
|
|
ml.onMsgComplete(cids[0], func(err error) {
|
2020-07-28 23:16:47 +00:00
|
|
|
count++
|
|
|
|
})
|
2020-08-10 21:52:59 +00:00
|
|
|
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)
|
|
|
|
}
|