Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
notifications-bot
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
archives
notifications-bot
Commits
f508f5d8
Commit
f508f5d8
authored
Apr 14, 2021
by
Richard T. Carback III
Browse files
Options
Downloads
Patches
Plain Diff
Update NDF with test
parent
75e11a7c
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
notifications/ndf.go
+23
-17
23 additions, 17 deletions
notifications/ndf.go
notifications/ndf_test.go
+42
-10
42 additions, 10 deletions
notifications/ndf_test.go
with
65 additions
and
27 deletions
notifications/ndf.go
+
23
−
17
View file @
f508f5d8
...
...
@@ -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
}
}
}
This diff is collapsed.
Click to expand it.
notifications/ndf_test.go
+
42
−
10
View file @
f508f5d8
...
...
@@ -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
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment