Skip to content

Commit

Permalink
Added tests for NT4 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Aug 21, 2023
1 parent 0fa4027 commit aa60611
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/services/nt4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class NT4Client {

WebSocketChannel? _ws;

Map<int, NT4Subscription> get subscriptions => _subscriptions;
Set<NT4Subscription> get subscribedTopics => _subscribedTopics;

NT4Client({
required this.serverBaseAddress,
this.onConnect,
Expand Down
8 changes: 8 additions & 0 deletions test/services/ds_interop_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:elastic_dashboard/services/ds_interop.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('DS Interop', () {
DSInteropClient();
});
}
60 changes: 60 additions & 0 deletions test/services/nt4_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:elastic_dashboard/services/nt4.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('NT4 Client', () {
bool connected = false;

NT4Client client = NT4Client(
serverBaseAddress: '10.3.53.2',
onConnect: () => connected = true,
onDisconnect: () => connected = false,
);

expect(connected, false);

// Subscribing
NT4Subscription subscription1 =
client.subscribe('/SmartDashboard/Test Number');

expect(client.subscriptions.length, greaterThanOrEqualTo(1));

expect(client.lastAnnouncedValues.isEmpty, true);

// Publishing and adding to the last announced values
client.addSample(
NT4Topic(
name: '/SmartDashboard/Test Number',
type: NT4TypeStr.kFloat32,
properties: {}),
3.53);

expect(client.lastAnnouncedValues.isEmpty, false);

expect(subscription1.currentValue != null, true);

NT4Subscription subscription2 =
client.subscribe('/SmartDashboard/Test Number');

// If the subscriptions are shared
expect(client.subscribedTopics.length, 1);

client.unSubscribe(subscription1);

expect(client.subscribedTopics.length, 1);

client.unSubscribe(subscription2);

expect(client.subscribedTopics.length, 0);

// Changing ip address
expect(client.lastAnnouncedValues.isEmpty, false);

client.setServerBaseAddreess('10.26.01.2');

expect(client.serverBaseAddress, '10.26.01.2');

expect(client.announcedTopics.isEmpty, true);
expect(client.lastAnnouncedValues.isEmpty, true);
});
}

0 comments on commit aa60611

Please sign in to comment.