package main import ( "testing" cid "github.com/ipfs/go-cid" mh "github.com/multiformats/go-multihash" "github.com/stretchr/testify/assert" ) func TestAppendCIDsToWindow(t *testing.T) { assert := assert.New(t) var window CidWindow threshold := 3 cid0 := makeCID("0") cid1 := makeCID("1") cid2 := makeCID("2") cid3 := makeCID("3") window = appendCIDsToWindow(window, []cid.Cid{cid0}, threshold) window = appendCIDsToWindow(window, []cid.Cid{cid1}, threshold) window = appendCIDsToWindow(window, []cid.Cid{cid2}, threshold) window = appendCIDsToWindow(window, []cid.Cid{cid3}, threshold) assert.Len(window, 3) assert.Equal(window[0][0], cid1) assert.Equal(window[1][0], cid2) assert.Equal(window[2][0], cid3) } func TestCheckWindow(t *testing.T) { assert := assert.New(t) ch := make(chan CidWindow, 1) och := make(chan bool, 1) threshold := 3 go func() { och <- checkWindow(ch, threshold) }() var healthyHeadCheckWindow CidWindow healthyHeadCheckWindow = appendCIDsToWindow(healthyHeadCheckWindow, []cid.Cid{ makeCID("abcd"), }, threshold) healthyHeadCheckWindow = appendCIDsToWindow(healthyHeadCheckWindow, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) healthyHeadCheckWindow = appendCIDsToWindow(healthyHeadCheckWindow, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) ch <- healthyHeadCheckWindow select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var healthyHeadCheckWindow1 CidWindow healthyHeadCheckWindow1 = appendCIDsToWindow(healthyHeadCheckWindow1, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) healthyHeadCheckWindow1 = appendCIDsToWindow(healthyHeadCheckWindow1, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), makeCID("abcd"), }, threshold) healthyHeadCheckWindow1 = appendCIDsToWindow(healthyHeadCheckWindow1, []cid.Cid{ makeCID("abcd"), }, threshold) ch <- healthyHeadCheckWindow1 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var healthyHeadCheckWindow2 CidWindow healthyHeadCheckWindow2 = appendCIDsToWindow(healthyHeadCheckWindow2, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) healthyHeadCheckWindow2 = appendCIDsToWindow(healthyHeadCheckWindow2, []cid.Cid{ makeCID("abcd"), }, threshold) ch <- healthyHeadCheckWindow2 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var healthyHeadCheckWindow3 CidWindow healthyHeadCheckWindow3 = appendCIDsToWindow(healthyHeadCheckWindow3, []cid.Cid{ makeCID("abcd"), }, threshold) healthyHeadCheckWindow3 = appendCIDsToWindow(healthyHeadCheckWindow3, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) ch <- healthyHeadCheckWindow3 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var healthyHeadCheckWindow4 CidWindow healthyHeadCheckWindow4 = appendCIDsToWindow(healthyHeadCheckWindow4, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, threshold) ch <- healthyHeadCheckWindow4 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, 5) }() var healthyHeadCheckWindow5 CidWindow healthyHeadCheckWindow5 = appendCIDsToWindow(healthyHeadCheckWindow5, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), makeCID("bbff"), }, 5) healthyHeadCheckWindow5 = appendCIDsToWindow(healthyHeadCheckWindow5, []cid.Cid{ makeCID("bbcd"), makeCID("bbfe"), }, 5) healthyHeadCheckWindow5 = appendCIDsToWindow(healthyHeadCheckWindow5, []cid.Cid{ makeCID("abcd"), }, 5) healthyHeadCheckWindow5 = appendCIDsToWindow(healthyHeadCheckWindow5, []cid.Cid{ makeCID("cbcd"), makeCID("cbfe"), }, 5) healthyHeadCheckWindow5 = appendCIDsToWindow(healthyHeadCheckWindow5, []cid.Cid{ makeCID("cbcd"), makeCID("cbfe"), }, 5) ch <- healthyHeadCheckWindow5 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var unhealthyHeadCheckWindow CidWindow unhealthyHeadCheckWindow = appendCIDsToWindow(unhealthyHeadCheckWindow, []cid.Cid{ makeCID("abcd"), makeCID("fbcd"), }, threshold) unhealthyHeadCheckWindow = appendCIDsToWindow(unhealthyHeadCheckWindow, []cid.Cid{ makeCID("abcd"), makeCID("fbcd"), }, threshold) unhealthyHeadCheckWindow = appendCIDsToWindow(unhealthyHeadCheckWindow, []cid.Cid{ makeCID("abcd"), makeCID("fbcd"), }, threshold) ch <- unhealthyHeadCheckWindow select { case ok := <-och: assert.False(ok) } go func() { och <- checkWindow(ch, threshold) }() var unhealthyHeadCheckWindow1 CidWindow unhealthyHeadCheckWindow1 = appendCIDsToWindow(unhealthyHeadCheckWindow1, []cid.Cid{ makeCID("abcd"), makeCID("fbcd"), }, threshold) unhealthyHeadCheckWindow1 = appendCIDsToWindow(unhealthyHeadCheckWindow1, []cid.Cid{ makeCID("abcd"), makeCID("fbcd"), }, threshold) ch <- unhealthyHeadCheckWindow1 select { case ok := <-och: assert.True(ok) } go func() { och <- checkWindow(ch, threshold) }() var unhealthyHeadCheckWindow2 CidWindow unhealthyHeadCheckWindow2 = appendCIDsToWindow(unhealthyHeadCheckWindow2, []cid.Cid{ makeCID("abcd"), }, threshold) unhealthyHeadCheckWindow2 = appendCIDsToWindow(unhealthyHeadCheckWindow2, []cid.Cid{ makeCID("abcd"), }, threshold) unhealthyHeadCheckWindow2 = appendCIDsToWindow(unhealthyHeadCheckWindow2, []cid.Cid{ makeCID("abcd"), }, threshold) ch <- unhealthyHeadCheckWindow2 select { case ok := <-och: assert.False(ok) } } func makeCID(s string) cid.Cid { h1, err := mh.Sum([]byte(s), mh.SHA2_256, -1) if err != nil { log.Fatal(err) } return cid.NewCidV1(0x55, h1) }