Skip to content

Commit

Permalink
Add a tailLogs method (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Jul 25, 2023
1 parent 597210c commit 6f95b70
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/src/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import '../gen/app/v1/app.pbgrpc.dart';

class AppClient {
Expand Down Expand Up @@ -52,4 +54,13 @@ class AppClient {
final response = await _client.getRobotPart(getRobotPartRequest);
return response.part;
}

Stream<List<LogEntry>> tailLogs(RobotPart part, {bool errorsOnly = false}) {
final request = TailRobotPartLogsRequest()
..id = part.id
..errorsOnly = errorsOnly;
final response = _client.tailRobotPartLogs(request);
final stream = response.map((event) => event.logs);
return stream.asBroadcastStream(onCancel: (_) => response.cancel());
}
}
2 changes: 1 addition & 1 deletion lib/src/viam_sdk_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViamImpl implements Viam {
late AppClient _appClient;
late DataClient _dataClient;

ViamImpl();
ViamImpl._();

ViamImpl.withAccessToken(String accessToken) : _clientChannelBase = AuthenticatedChannel('app.viam.com', 443, accessToken, false) {
_appClient = AppClient(AppServiceClient(_clientChannelBase!));
Expand Down
13 changes: 13 additions & 0 deletions test/unit_test/app/app_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,18 @@ void main() {
final response = await appClient.getRobotPart('robot part');
expect(response, equals(expected));
});

test('tailLogs', () async {
final expected = LogEntry()..message = 'My log entry';
final response = TailRobotPartLogsResponse()..logs.add(expected);
when(serviceClient.tailRobotPartLogs(any)).thenAnswer((_) => MockResponseStream.list([response]));
final stream = appClient.tailLogs(RobotPart());
expect(
stream,
emitsInOrder([
emits([expected]),
emitsDone
]));
});
});
}
27 changes: 27 additions & 0 deletions test/unit_test/mocks/mock_response_future.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,30 @@ class MockResponseFuture<T> extends Mock implements ResponseFuture<T> {
@override
Future<S> then<S>(FutureOr<S> Function(T) onValue, {Function? onError}) => future.then(onValue, onError: onError);
}

class MockResponseStream<T> extends Mock implements ResponseStream<T> {
final Stream<T> stream;

MockResponseStream.stream(this.stream);

MockResponseStream.list(List<T> list) : stream = Stream.fromIterable(list);

MockResponseStream.future(Future<T> future) : stream = Stream.fromFuture(future);

MockResponseStream.futures(List<Future<T>> futures) : stream = Stream.fromFutures(futures);

@override
StreamSubscription<T> listen(void Function(T value)? onData, {Function? onError, void Function()? onDone, bool? cancelOnError}) {
return stream.listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);
}

@override
Future<void> cancel() {
return Future.value();
}

@override
Stream<S> map<S>(S Function(T event) convert) {
return stream.map(convert);
}
}

0 comments on commit 6f95b70

Please sign in to comment.