-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSDK-7769, RSDK-7770 Add AppRobotClient and LogOutput dial option (#233)
- Loading branch information
1 parent
602498e
commit c832369
Showing
9 changed files
with
1,065 additions
and
604 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:logger/logger.dart'; | ||
import 'package:viam_sdk/src/gen/google/protobuf/timestamp.pb.dart'; | ||
|
||
import '../gen/app/v1/robot.pbgrpc.dart'; | ||
import '../gen/common/v1/common.pb.dart'; | ||
|
||
/// gRPC client for connecting to app's RobotService. | ||
/// | ||
/// All calls must be authenticated. | ||
class AppRobotClient { | ||
final RobotServiceClient _client; | ||
|
||
AppRobotClient(this._client); | ||
|
||
/// Log the OutputEvent to app with the given partId. | ||
Future<void> log(String partId, host, loggerName, OutputEvent event) async { | ||
late String level; | ||
switch (event.level) { | ||
case Level.debug: | ||
level = 'debug'; | ||
case Level.warning: | ||
level = 'warning'; | ||
case Level.error: | ||
level = 'error'; | ||
default: | ||
// Assume info level if none of the above. | ||
level = 'info'; | ||
} | ||
|
||
// Assume log was just output (OutputEvent has no timestamp field). | ||
final Timestamp protoTs = Timestamp.fromDateTime(DateTime.now()); | ||
|
||
// Join lines with '\n' and suffix with '\n'. | ||
final String message = '${event.lines.join('\n')}\n'; | ||
|
||
final LogEntry entry = LogEntry(host: host, level: level, time: protoTs, loggerName: loggerName, message: message); | ||
final request = LogRequest(id: partId, logs: [entry]); | ||
await _client.log(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:logger/logger.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:viam_sdk/src/gen/app/v1/robot.pbgrpc.dart'; | ||
import 'package:viam_sdk/viam_sdk.dart'; | ||
|
||
import '../mocks/mock_response_future.dart'; | ||
import '../mocks/service_clients_mocks.mocks.dart'; | ||
|
||
void main() { | ||
late MockAppRobotServiceClient serviceClient; | ||
late AppRobotClient appRobotClient; | ||
|
||
setUp(() { | ||
serviceClient = MockAppRobotServiceClient(); | ||
appRobotClient = AppRobotClient(serviceClient); | ||
}); | ||
|
||
group('App Robot RPC Client Tests', () { | ||
test('log', () async { | ||
when(serviceClient.log(any)).thenAnswer((_) => MockResponseFuture.value(LogResponse())); | ||
|
||
final event = LogEvent(Level.info, 'fake log message'); | ||
await appRobotClient.log('fakePartId', 'fakeHost', 'fakeLoggerName', OutputEvent(event, List.empty())); | ||
verify(serviceClient.log(any)).called(1); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:logger/logger.dart'; | ||
import 'package:viam_sdk/viam_sdk.dart'; | ||
|
||
class TestLogOutput extends LogOutput { | ||
final List<OutputEvent> _outputtedEvents = List.empty(growable: true); | ||
TestLogOutput(); | ||
|
||
@override | ||
void output(OutputEvent event) { | ||
_outputtedEvents.add(event); | ||
} | ||
|
||
List<OutputEvent> outputtedEvents() { | ||
return _outputtedEvents; | ||
} | ||
} | ||
|
||
void main() { | ||
group('dial', () { | ||
test('custom log output', () { | ||
final output = TestLogOutput(); | ||
final dialOpts = DialOptions()..logOutput = output; | ||
// Dial to arbitrary address and assert that logs get redirected to TestLogOutput. | ||
dial('foo', dialOpts, () => ''); | ||
|
||
final events = output.outputtedEvents(); | ||
expect(events.isNotEmpty, true); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.