Skip to content
Snippets Groups Projects
Commit f508f5d8 authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Update NDF with test

parent 75e11a7c
No related branches found
No related tags found
No related merge requests found
......@@ -60,42 +60,48 @@ func TrackNdf(i *network.Instance, c *notificationBot.Comms) Stopper {
}
func trackNdf(poller io.PollingConn, quitCh chan bool, gwEvt GatewaysChanged) {
lastNdfHash := make([]byte, 32)
pollDelay := 1 * time.Second
hashCh := make(chan []byte, 1)
lastNdf := pb.NDF{Ndf: []byte{}}
lastNdfHash := []byte{}
for {
jww.TRACE.Printf("Polling for NDF")
// FIXME: This is mildly hacky because we rely on the call back
// to return the ndf hash right now.
select {
case newHash := <-hashCh:
lastNdfHash = newHash
default:
break
}
ndf, err := poller.PollNdf(lastNdfHash)
if err != nil {
jww.ERROR.Printf("polling ndf: %+v", err)
time.Sleep(pollDelay)
continue
ndf = nil
}
// If the cur Hash differs from the last one, trigger the update
// If the cur differs from the last one, trigger the update
// event
// TODO: Improve this to only trigger when gatways are updated
// this isn't useful right now because gw event handlers
// actually update the full ndf each time, so it's a
// choice between comparing the full hash or additional
// network traffic given the current state of API.
// FIXME: This is mildly hacky because we rely on the call back
// to return the ndf hash right now.
curNdfHash := lastNdfHash
select {
case hashUpdate := <-hashCh:
curNdfHash = hashUpdate
default:
break
}
if bytes.Equal(curNdfHash, lastNdfHash) {
if ndf != nil && !bytes.Equal(ndf.Ndf, lastNdf.Ndf) {
// FIXME: we should be able to get hash from the ndf
// object, but we can't.
go func() { hashCh <- gwEvt(*ndf) }()
lastNdf = *ndf
}
lastNdfHash = curNdfHash
time.Sleep(pollDelay)
select {
case <-quitCh:
jww.DEBUG.Printf("Exiting trackNDF thread...")
return
case <-time.After(pollDelay):
continue
}
}
}
......@@ -8,23 +8,25 @@
package notifications
import (
"bytes"
pb "gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/comms/testutils"
"sync"
"testing"
"time"
)
type MockPoller struct {
ndf *pb.NDF
ndf pb.NDF
sync.Mutex
}
func (m MockPoller) PollNdf(ndfHash []byte) (*pb.NDF, error) {
func (m *MockPoller) PollNdf(ndfHash []byte) (*pb.NDF, error) {
m.Lock()
defer m.Unlock()
return m.ndf, nil
return &m.ndf, nil
}
func (m MockPoller) UpdateNdf(newNDF *pb.NDF) {
func (m *MockPoller) UpdateNdf(newNDF pb.NDF) {
m.Lock()
defer m.Unlock()
m.ndf = newNDF
......@@ -35,24 +37,54 @@ func TestTrackNdf(t *testing.T) {
// Stopping function for the thread
quitCh := make(chan bool)
poller := MockPoller{
ndf: nil,
startNDF := pb.NDF{Ndf: make([]byte, 10)}
copy(startNDF.Ndf, testutils.ExampleNDF[0:10])
newNDF := pb.NDF{Ndf: make([]byte, 10)}
copy(newNDF.Ndf, testutils.ExampleNDF[0:10])
poller := &MockPoller{
ndf: startNDF,
}
gwUpdates := 0
lastNdf := make([]byte, 10)
gatewayEventHandler := func(ndf pb.NDF) []byte {
t.Logf("Updating Gateways with new NDF")
gwUpdates += 1
return nil
t.Logf("%v == %v?", ndf.Ndf, lastNdf)
if !bytes.Equal(lastNdf, ndf.Ndf) {
t.Logf("Incrementing counter")
copy(lastNdf, ndf.Ndf)
gwUpdates++
}
// We control the hash, so we control the update calls...
ndfHash := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, byte(gwUpdates % 255)}
return ndfHash
}
go trackNdf(poller, quitCh, gatewayEventHandler)
// 3 changes, starting change
time.Sleep(100 * time.Millisecond)
// 2nd change Start -> newNDF
newNDF.Ndf[5] = byte('a')
poller.UpdateNdf(newNDF)
time.Sleep(1100 * time.Millisecond)
// 3rd change newNDF -> startNDF
poller.UpdateNdf(startNDF)
time.Sleep(1100 * time.Millisecond)
select {
case <-time.After(2 * time.Second):
t.Errorf("Could not stop NDF Tracking Thread")
case quitCh <- true:
break
case <-time.After(2 * time.Second):
t.Errorf("Could not stop NDF Tracking Thread")
}
if gwUpdates != 3 {
t.Errorf("updates not detected, expected 3 got: %d", gwUpdates)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment