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
d26ec3d9
Commit
d26ec3d9
authored
Apr 13, 2021
by
Richard T. Carback III
Browse files
Options
Downloads
Patches
Plain Diff
Add NDF thread with initial (failing) test for feedback
parent
a286d63a
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
+92
-0
92 additions, 0 deletions
notifications/ndf.go
notifications/ndf_test.go
+57
-0
57 additions, 0 deletions
notifications/ndf_test.go
with
149 additions
and
0 deletions
notifications/ndf.go
0 → 100644
+
92
−
0
View file @
d26ec3d9
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
// ndf controls gateway updates from the permissioning server
package
notifications
import
(
//"github.com/pkg/errors"
jww
"github.com/spf13/jwalterweatherman"
pb
"gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/notifications-bot/io"
"gitlab.com/xx_network/comms/connect"
//"gitlab.com/elixxir/comms/notificationBot"
"time"
"bytes"
"gitlab.com/elixxir/crypto/hash"
)
// Stopper function that stops the thread on a timeout
type
Stopper
func
(
timeout
time
.
Duration
)
bool
// GatewaysChanged function processes the gateways changed event when detected
// in the NDF
type
GatewaysChanged
func
(
ndf
pb
.
NDF
)
// InstanceObject is a mock of the instance object...
type
InstanceObject
interface
{
UpdateGateways
(
ndf
*
pb
.
NDF
)
GetProtoComms
()
*
connect
.
ProtoComms
GetPermHost
()
*
connect
.
Host
}
// TrackNdf kicks off the ndf tracking thread
func
TrackNdf
(
i
InstanceObject
)
Stopper
{
// Handler function for the gateways changed event
gatewayEventHandler
:=
func
(
ndf
pb
.
NDF
)
{
jww
.
DEBUG
.
Printf
(
"Updating Gateways with new NDF"
)
// TODO: If this returns an error, print that error if it occurs
i
.
UpdateGateways
(
&
ndf
)
}
// Stopping function for the thread
quitCh
:=
make
(
chan
bool
)
quitFn
:=
func
(
timeout
time
.
Duration
)
bool
{
select
{
case
quitCh
<-
true
:
return
true
case
<-
time
.
After
(
timeout
)
:
jww
.
ERROR
.
Printf
(
"Could not stop NDF Tracking Thread"
)
return
false
}
}
// Polling object
poller
:=
io
.
NewNdfPoller
(
i
.
GetProtoComms
(),
i
.
GetPermHost
())
go
trackNdf
(
poller
,
quitCh
,
gatewayEventHandler
)
return
quitFn
}
func
trackNdf
(
poller
io
.
PollingConn
,
quitCh
chan
bool
,
gwEvt
GatewaysChanged
)
{
lastNdfHash
:=
make
([]
byte
,
32
)
pollDelay
:=
1
*
time
.
Second
cMixHash
,
_
:=
hash
.
NewCMixHash
()
nonce
:=
make
([]
byte
,
32
)
for
{
jww
.
TRACE
.
Printf
(
"Polling for NDF"
)
ndf
,
err
:=
poller
.
PollNdf
()
if
err
!=
nil
{
jww
.
ERROR
.
Printf
(
"polling ndf: %+v"
,
err
)
time
.
Sleep
(
pollDelay
)
}
// If the cur Hash differs from the last one, trigger the update
// event
// TODO: Improve this to only trigger when gatways are updated
curNdfHash
:=
ndf
.
Digest
(
nonce
,
cMixHash
)
if
bytes
.
Equal
(
curNdfHash
,
lastNdfHash
)
{
go
gwEvt
(
*
ndf
)
}
lastNdfHash
=
curNdfHash
time
.
Sleep
(
pollDelay
)
}
}
This diff is collapsed.
Click to expand it.
notifications/ndf_test.go
0 → 100644
+
57
−
0
View file @
d26ec3d9
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
package
notifications
import
(
"testing"
pb
"gitlab.com/elixxir/comms/mixmessages"
"sync"
"time"
)
type
MockPoller
struct
{
ndf
*
pb
.
NDF
sync
.
Mutex
}
func
(
m
MockPoller
)
PollNdf
()
(
*
pb
.
NDF
,
error
)
{
m
.
Lock
()
defer
m
.
Unlock
()
return
m
.
ndf
,
nil
}
func
(
m
MockPoller
)
UpdateNdf
(
newNDF
*
pb
.
NDF
)
{
m
.
Lock
()
defer
m
.
Unlock
()
m
.
ndf
=
newNDF
}
// TestTrackNdf performs a basic test of the trackNdf function
func
TestTrackNdf
(
t
*
testing
.
T
)
{
// Stopping function for the thread
quitCh
:=
make
(
chan
bool
)
poller
:=
MockPoller
{
ndf
:
nil
,
}
gwUpdates
:=
0
gatewayEventHandler
:=
func
(
ndf
pb
.
NDF
)
{
t
.
Logf
(
"Updating Gateways with new NDF"
)
gwUpdates
+=
1
}
go
trackNdf
(
poller
,
quitCh
,
gatewayEventHandler
)
select
{
case
<-
time
.
After
(
2
*
time
.
Second
)
:
t
.
Errorf
(
"Could not stop NDF Tracking Thread"
)
case
quitCh
<-
true
:
break
}
}
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