-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Network Server GetDevAddrAssignment RPC #758
Network Server GetDevAddrAssignment RPC #758
Conversation
api/networkserver.proto
Outdated
|
||
service Ns { | ||
// GetDevAddrAssignment requests a device address assignment from the Network Server. | ||
rpc GetDevAddrAssignment(GetDevAddrAssignmentRequest) returns (GetDevAddrAssignmentResponse) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Why not just use
google.protobuf.Empty
as the request here? - IMO
Get
here implies that you're getting something that already exists, however that's not the case - DevAddr is generated when this RPC is called. How aboutAssignDevAddr
,CreateDevAddr
,GenerateDevAddr
?
pkg/networkserver/grpc_ns_test.go
Outdated
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package networkserver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be networkserver_test
pkg/networkserver/grpc_ns_test.go
Outdated
}, | ||
})).(*NetworkServer) | ||
|
||
if !a.So(ns.devAddrPrefixes, should.HaveLength, 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not belong here
pkg/networkserver/grpc_ns_test.go
Outdated
if !a.So(ns.devAddrPrefixes, should.HaveLength, 1) { | ||
t.FailNow() | ||
} | ||
a.So(ns.devAddrPrefixes[0], should.Resemble, types.DevAddrPrefix{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEM
pkg/networkserver/grpc_ns_test.go
Outdated
}) | ||
|
||
devAddr, _ := ns.GetDevAddrAssignment(test.Context(), nil) | ||
a.So(devAddr.DevAddr.HasPrefix(ns.devAddrPrefixes[0]), should.BeTrue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specify the prefix explicitly
pkg/networkserver/grpc_ns_test.go
Outdated
} | ||
} | ||
} | ||
a.So(seen[ns.devAddrPrefixes[0]], should.BeGreaterThan, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is non-deterministic, right? Eventually this test will start consistently failing in CI, since the RNG will omit one of the prefixes.
Check each devAddr returned explicitly - i.e. in the loop assert that it has one of the prefixes configured above in the config.
Also, do not use internal NS fields(ns.devAddrPrefixes
)
pkg/networkserver/grpc_ns_test.go
Outdated
seen[testPrefix3]++ | ||
} | ||
} | ||
a.So(seen[testPrefix1], should.BeGreaterThan, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still non-deterministic, drop the seen
and assert the prefix in the loop per each generated DevAddr.(i.e. assert that each generated DevAddr has one of the 3 prefixes specified)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes from my suggested code where we test the generation itself. I agree that we should assert that, but also that all prefixes are present. With N of 100 that, statistically, must be the case if the RNG isn’t broken. I’d like to test that. We can increase N though, but all prefixes must be there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what would be the best way to resolve that, if we talk about assertion AND check if all of the prefixes present? add assertion without dropping the seen
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, or sum the seen and check if that number matches the number of generated, or fatal the test if the prefix doesn't match in the loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't need to test the RNG here, right?
Why not just do this on L84
:
hasOneOfPrefixes := func(devAddr types.DevAddr, prefixes ...string) bool {
for _, p := range prefixes {
if devAddr.HasPrefix(p) {
return true
}
}
return false
}
a.So(hasOneOfPrefixes(devAddr.DevAddr, testPrefix1, testPrefix2, testPrefix3), should.BeTrue)
It's obvious what happens here and it's easy to debug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we don't need to test the RNG here, right?
We need to test if all the configured prefixes are considered. That is complementary to testing whether all DevAddrs fit in one of the configured prefixes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to test that NS actually uses the RNG, then you can leave the seen
assertions as is, but you do need to assert that devAddr generated is actually valid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is the long version of "complementary" 😉
pkg/networkserver/grpc_ns_test.go
Outdated
}, | ||
})).(*NetworkServer) | ||
|
||
devAddr, _ := ns.GenerateDevAddrAssignment(test.Context(), nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call this via the gRPC client(https://github.com/TheThingsNetwork/lorawan-stack/pull/758/files#diff-e699b03edbcaf36d0a1bd906c75d1d22R584)
Look at the registry tests for an example on how to do that.
api/networkserver.proto
Outdated
} | ||
|
||
service Ns { | ||
// GetDevAddrAssignment requests a device address assignment from the Network Server. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong comment
api/networkserver.proto
Outdated
@@ -69,3 +69,16 @@ service NsEndDeviceRegistry { | |||
}; | |||
} | |||
|
|||
message GetDevAddrAssignmentResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong name(does not reflect the rename)
api/networkserver.proto
Outdated
|
||
service Ns { | ||
// GetDevAddrAssignment requests a device address assignment from the Network Server. | ||
rpc GenerateDevAddrAssignment(google.protobuf.Empty) returns (GetDevAddrAssignmentResponse) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just GenerateDevAddr
?
1 - In the
when I try to use the 2 - I have rewritten the tests and now they indeed include both of the discussed complementary properties ;) |
api/networkserver.proto
Outdated
@@ -69,3 +69,20 @@ service NsEndDeviceRegistry { | |||
}; | |||
} | |||
|
|||
message GenerateDevAddrRequest { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should either use google.protobuf.Empty
in the RPC or this message should contain a TODO
with references to issues, which describe what fields are missing here
pkg/networkserver/grpc_ns_test.go
Outdated
DevAddrPrefix types.DevAddrPrefix | ||
}{ | ||
{ | ||
Name: "Prefix from NS NedID 1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NetID
?
Also other occurences
pkg/networkserver/grpc_ns_test.go
Outdated
"go.thethings.network/lorawan-stack/pkg/util/test/assertions/should" | ||
) | ||
|
||
func pureNewDevAddr(netID types.NetID) types.DevAddr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant, just use test.Must(types.NewDevAddr).(types.DevAddr)
Also, the naming is mustX
or MustX
, e.g. mustNewDevAddr
pkg/networkserver/grpc_ns_test.go
Outdated
test.Must(nil, ns.Start()) | ||
defer ns.Close() | ||
|
||
devAddr, _ := ttnpb.NewNsClient(ns.LoopbackConn()).GenerateDevAddr(test.Context(), nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert that error
is nil
pkg/networkserver/grpc_ns_test.go
Outdated
|
||
seen := map[types.DevAddrPrefix]int{} | ||
for i := 0; i < 100; i++ { | ||
devAddr, _ := ttnpb.NewNsClient(ns.LoopbackConn()).GenerateDevAddr(test.Context(), nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEM
pkg/networkserver/grpc_ns_test.go
Outdated
}, | ||
})).(*NetworkServer) | ||
|
||
ns.AddContextFiller(func(ctx context.Context) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant
pkg/networkserver/grpc_ns_test.go
Outdated
_ = cancel | ||
return ctx | ||
}) | ||
ns.AddContextFiller(func(ctx context.Context) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant
pkg/networkserver/grpc_ns_test.go
Outdated
}, | ||
})).(*NetworkServer) | ||
|
||
ns.AddContextFiller(func(ctx context.Context) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEM
pkg/networkserver/grpc_ns_test.go
Outdated
_ = cancel | ||
return ctx | ||
}) | ||
ns.AddContextFiller(func(ctx context.Context) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDEM
You probably returned |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
"go.thethings.network/lorawan-stack/pkg/util/test/assertions/should" | ||
) | ||
|
||
func TestGenerateDevAddrAssignment(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestGenerateDevAddr
|
||
"github.com/smartystreets/assertions" | ||
"go.thethings.network/lorawan-stack/pkg/component" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be no empty line here
Summary
Closes #294
Changes
grpc_ns.go
andgrpc_ns_test.go
filesGenerateDevAddrAssignment
rpc ingrpc_ns.go
TestGenerateDevAddrAssignment
test ingrpc_ns_test.go
.Notes for Reviewers
Props @htdvisser for helping out