diff --git a/lib/src/internal/data_client.dart b/lib/src/internal/data_client.dart index be24f31..64c3952 100644 --- a/lib/src/internal/data_client.dart +++ b/lib/src/internal/data_client.dart @@ -179,7 +179,9 @@ class DataClient implements AbstractDataClient { options: CallOptions(metadata: { 'cache': cacheName, })); - return ListConcatenateFrontSuccess(response.listLength); + return ListConcatenateFrontSuccess( + response.listLength, + ); } catch (e) { if (e is GrpcError) { return ListConcatenateFrontError(grpcStatusToSdkException(e)); @@ -271,7 +273,9 @@ class DataClient implements AbstractDataClient { })); switch (response.whichList()) { case ListPopBackResponse__List.found: - return ListPopBackHit(response.found.back); + return ListPopBackHit( + response.found.back, + ); case ListPopBackResponse__List.missing: return ListPopBackMiss(); default: diff --git a/lib/src/internal/utils/display.dart b/lib/src/internal/utils/display.dart new file mode 100644 index 0000000..2da91df --- /dev/null +++ b/lib/src/internal/utils/display.dart @@ -0,0 +1,27 @@ +const displaySizeLimit = 5; + +String truncateString(String value, {int maxLength = 32}) { + if (value.length > maxLength) { + return '${value.substring(0, maxLength)}...'; + } else { + return value; + } +} + +List truncateStringArrayToSize(List stringArray, + {int length = displaySizeLimit}) { + if (stringArray.length <= length) { + return stringArray; + } else { + return stringArray.sublist(0, length).followedBy(['...']).toList(); + } +} + +List truncateStringArray(List stringArray, + {int length = displaySizeLimit}) { + final truncatedStringArray = + truncateStringArrayToSize(stringArray, length: length); + return truncatedStringArray.map((s) { + return truncateString(s); + }).toList(); +} diff --git a/lib/src/messages/responses/cache/control/create_cache_response.dart b/lib/src/messages/responses/cache/control/create_cache_response.dart index dbb4899..881cd35 100644 --- a/lib/src/messages/responses/cache/control/create_cache_response.dart +++ b/lib/src/messages/responses/cache/control/create_cache_response.dart @@ -16,10 +16,11 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class CreateCacheResponse {} /// Indicates a successful create cache request. -class CreateCacheSuccess implements CreateCacheResponse {} +class CreateCacheSuccess extends ResponseBase implements CreateCacheResponse {} /// Indicates that the cache already exists, so there was nothing to do. -class CreateCacheAlreadyExists implements CreateCacheResponse {} +class CreateCacheAlreadyExists extends ResponseBase + implements CreateCacheResponse {} /// Indicates that an error occurred during the create cache request. /// diff --git a/lib/src/messages/responses/cache/control/delete_cache_response.dart b/lib/src/messages/responses/cache/control/delete_cache_response.dart index 11c00bd..efc1094 100644 --- a/lib/src/messages/responses/cache/control/delete_cache_response.dart +++ b/lib/src/messages/responses/cache/control/delete_cache_response.dart @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class DeleteCacheResponse {} /// Indicates a successful delete cache request. -class DeleteCacheSuccess implements DeleteCacheResponse {} +class DeleteCacheSuccess extends ResponseBase implements DeleteCacheResponse {} /// Indicates that an error occurred during the delete cache request. /// diff --git a/lib/src/messages/responses/cache/control/list_caches_response.dart b/lib/src/messages/responses/cache/control/list_caches_response.dart index b149dcb..969c54a 100644 --- a/lib/src/messages/responses/cache/control/list_caches_response.dart +++ b/lib/src/messages/responses/cache/control/list_caches_response.dart @@ -1,4 +1,5 @@ import 'package:momento/generated/controlclient.pb.dart'; +import 'package:momento/src/internal/utils/display.dart'; import 'package:momento/src/messages/responses/responses_base.dart'; /// Represents information about a listed cache, such as its name. @@ -23,7 +24,7 @@ class CacheInfo { sealed class ListCachesResponse {} /// Indicates a successful list caches request. -class ListCachesSuccess implements ListCachesResponse { +class ListCachesSuccess extends ResponseBase implements ListCachesResponse { late final List caches; ListCachesSuccess(List grpcCaches) { @@ -35,6 +36,11 @@ class ListCachesSuccess implements ListCachesResponse { } List get cacheNames => caches.map((cache) => cache.name).toList(); + + @override + String toString() { + return "${super.toString()}: ${truncateStringArray(cacheNames)}"; + } } /// Indicates that an error occurred during the list caches request. diff --git a/lib/src/messages/responses/cache/data/list/list_concatenate_back.dart b/lib/src/messages/responses/cache/data/list/list_concatenate_back.dart index f0adcdf..b8154f8 100644 --- a/lib/src/messages/responses/cache/data/list/list_concatenate_back.dart +++ b/lib/src/messages/responses/cache/data/list/list_concatenate_back.dart @@ -14,12 +14,18 @@ import '../../../responses_base.dart'; sealed class ListConcatenateBackResponse {} /// Indicates that the request was successful and the updated length can be accessed by the field `length`. -class ListConcatenateBackSuccess implements ListConcatenateBackResponse { +class ListConcatenateBackSuccess extends ResponseBase + implements ListConcatenateBackResponse { ListConcatenateBackSuccess(this._length); final int _length; int get length => _length; + + @override + String toString() { + return "${super.toString()}: Length $length"; + } } /// Indicates that an error occurred during the list concatenate back request. diff --git a/lib/src/messages/responses/cache/data/list/list_concatenate_front.dart b/lib/src/messages/responses/cache/data/list/list_concatenate_front.dart index a65683d..333661b 100644 --- a/lib/src/messages/responses/cache/data/list/list_concatenate_front.dart +++ b/lib/src/messages/responses/cache/data/list/list_concatenate_front.dart @@ -14,12 +14,18 @@ import '../../../responses_base.dart'; sealed class ListConcatenateFrontResponse {} /// Indicates that the request was successful and the updated length can be accessed by the field `length`. -class ListConcatenateFrontSuccess implements ListConcatenateFrontResponse { +class ListConcatenateFrontSuccess extends ResponseBase + implements ListConcatenateFrontResponse { ListConcatenateFrontSuccess(this._length); final int _length; int get length => _length; + + @override + String toString() { + return "${super.toString()}: Length $length"; + } } /// Indicates that an error occurred during the list concatenate front request. diff --git a/lib/src/messages/responses/cache/data/list/list_fetch.dart b/lib/src/messages/responses/cache/data/list/list_fetch.dart index 0a874f0..b6f5dec 100644 --- a/lib/src/messages/responses/cache/data/list/list_fetch.dart +++ b/lib/src/messages/responses/cache/data/list/list_fetch.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:momento/src/internal/utils/display.dart'; import 'package:momento/src/messages/responses/responses_base.dart'; /// Sealed class for a cache list fetch response. @@ -18,7 +19,7 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class ListFetchResponse {} /// Indicates that the requested list was not available in the cache. -class ListFetchMiss implements ListFetchResponse {} +class ListFetchMiss extends ResponseBase implements ListFetchResponse {} /// Indicates that an error occurred during the list fetch request. /// @@ -31,11 +32,16 @@ class ListFetchError extends ErrorResponseBase implements ListFetchResponse { } /// Indicates that the requested list was successfully retrieved from the cache and can be accessed by the fields `values` or `binaryValues`. -class ListFetchHit implements ListFetchResponse { +class ListFetchHit extends ResponseBase implements ListFetchResponse { ListFetchHit(this._values); final List> _values; Iterable get values => _values.map((e) => utf8.decode(e)); Iterable> get binaryValues => _values; + + @override + String toString() { + return "${super.toString()}: ${truncateStringArray(values.toList())}"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_length.dart b/lib/src/messages/responses/cache/data/list/list_length.dart index c5cba1d..a55331e 100644 --- a/lib/src/messages/responses/cache/data/list/list_length.dart +++ b/lib/src/messages/responses/cache/data/list/list_length.dart @@ -16,7 +16,7 @@ import '../../../responses_base.dart'; sealed class ListLengthResponse {} /// Indicates that the requested list was not available in the cache. -class ListLengthMiss implements ListLengthResponse {} +class ListLengthMiss extends ResponseBase implements ListLengthResponse {} /// Indicates that an error occurred during the list length request. /// @@ -29,10 +29,15 @@ class ListLengthError extends ErrorResponseBase implements ListLengthResponse { } /// Indicates that the requested list length was successfully retrieved from the cache and the length can be accessed by the field `length`. -class ListLengthHit implements ListLengthResponse { +class ListLengthHit extends ResponseBase implements ListLengthResponse { ListLengthHit(this._length); final int _length; int get length => _length; + + @override + String toString() { + return "${super.toString()}: Length $length"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_pop_back.dart b/lib/src/messages/responses/cache/data/list/list_pop_back.dart index b083661..d5bbe5f 100644 --- a/lib/src/messages/responses/cache/data/list/list_pop_back.dart +++ b/lib/src/messages/responses/cache/data/list/list_pop_back.dart @@ -1,5 +1,7 @@ import 'dart:convert'; +import 'package:momento/src/internal/utils/display.dart'; + import '../../../responses_base.dart'; /// Sealed class for a cache list pop back response. @@ -18,7 +20,7 @@ import '../../../responses_base.dart'; sealed class ListPopBackResponse {} /// Indicates that the requested list was not available in the cache. -class ListPopBackMiss implements ListPopBackResponse {} +class ListPopBackMiss extends ResponseBase implements ListPopBackResponse {} /// Indicates that an error occurred during the list pop back request. /// @@ -32,11 +34,16 @@ class ListPopBackError extends ErrorResponseBase } /// Indicates that the request was successful and the value can be accessed by the fields `value` or `binaryValue`. -class ListPopBackHit implements ListPopBackResponse { +class ListPopBackHit extends ResponseBase implements ListPopBackResponse { ListPopBackHit(this._value); final List _value; String get value => utf8.decode(_value); List get binaryValue => _value; + + @override + String toString() { + return "${super.toString()}: ${truncateString(value)}"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_pop_front.dart b/lib/src/messages/responses/cache/data/list/list_pop_front.dart index 6160d2e..2b19298 100644 --- a/lib/src/messages/responses/cache/data/list/list_pop_front.dart +++ b/lib/src/messages/responses/cache/data/list/list_pop_front.dart @@ -1,5 +1,7 @@ import 'dart:convert'; +import 'package:momento/src/internal/utils/display.dart'; + import '../../../responses_base.dart'; /// Sealed class for a cache list pop front response. @@ -18,7 +20,7 @@ import '../../../responses_base.dart'; sealed class ListPopFrontResponse {} /// Indicates that the requested list was not available in the cache. -class ListPopFrontMiss implements ListPopFrontResponse {} +class ListPopFrontMiss extends ResponseBase implements ListPopFrontResponse {} /// Indicates that an error occurred during the list pop front request. /// @@ -32,11 +34,16 @@ class ListPopFrontError extends ErrorResponseBase } /// Indicates that the request was successful and the value can be accessed by the fields `value` or `binaryValue`. -class ListPopFrontHit implements ListPopFrontResponse { +class ListPopFrontHit extends ResponseBase implements ListPopFrontResponse { ListPopFrontHit(this._value); final List _value; String get value => utf8.decode(_value); List get binaryValue => _value; + + @override + String toString() { + return "${super.toString()}: ${truncateString(value)}"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_push_back.dart b/lib/src/messages/responses/cache/data/list/list_push_back.dart index 546b498..6825053 100644 --- a/lib/src/messages/responses/cache/data/list/list_push_back.dart +++ b/lib/src/messages/responses/cache/data/list/list_push_back.dart @@ -25,10 +25,15 @@ class ListPushBackError extends ErrorResponseBase } /// Indicates that the request was successful and the updated length can be accessed by the field `length`. -class ListPushBackSuccess implements ListPushBackResponse { +class ListPushBackSuccess extends ResponseBase implements ListPushBackResponse { ListPushBackSuccess(this._length); final int _length; int get length => _length; + + @override + String toString() { + return "${super.toString()}: Length $length"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_push_front.dart b/lib/src/messages/responses/cache/data/list/list_push_front.dart index d104478..85acac4 100644 --- a/lib/src/messages/responses/cache/data/list/list_push_front.dart +++ b/lib/src/messages/responses/cache/data/list/list_push_front.dart @@ -25,10 +25,16 @@ class ListPushFrontError extends ErrorResponseBase } /// Indicates that the request was successful and the updated length can be accessed by the field `length`. -class ListPushFrontSuccess implements ListPushFrontResponse { +class ListPushFrontSuccess extends ResponseBase + implements ListPushFrontResponse { ListPushFrontSuccess(this._length); final int _length; int get length => _length; + + @override + String toString() { + return "${super.toString()}: Length $length"; + } } diff --git a/lib/src/messages/responses/cache/data/list/list_remove_value.dart b/lib/src/messages/responses/cache/data/list/list_remove_value.dart index e0317de..b422da2 100644 --- a/lib/src/messages/responses/cache/data/list/list_remove_value.dart +++ b/lib/src/messages/responses/cache/data/list/list_remove_value.dart @@ -14,7 +14,8 @@ import '../../../responses_base.dart'; sealed class ListRemoveValueResponse {} /// Indicates that the request was successful. -class ListRemoveValueSuccess implements ListRemoveValueResponse {} +class ListRemoveValueSuccess extends ResponseBase + implements ListRemoveValueResponse {} /// Indicates that an error occurred during the list remove value request. /// diff --git a/lib/src/messages/responses/cache/data/list/list_retain.dart b/lib/src/messages/responses/cache/data/list/list_retain.dart index 7440d15..2137c19 100644 --- a/lib/src/messages/responses/cache/data/list/list_retain.dart +++ b/lib/src/messages/responses/cache/data/list/list_retain.dart @@ -14,7 +14,7 @@ import '../../../responses_base.dart'; sealed class ListRetainResponse {} /// Indicates that the request was successful. -class ListRetainSuccess implements ListRetainResponse {} +class ListRetainSuccess extends ResponseBase implements ListRetainResponse {} /// Indicates that an error occurred during the list retain request. /// diff --git a/lib/src/messages/responses/cache/data/scalar/delete_response.dart b/lib/src/messages/responses/cache/data/scalar/delete_response.dart index afd3108..92325ff 100644 --- a/lib/src/messages/responses/cache/data/scalar/delete_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/delete_response.dart @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class DeleteResponse {} /// Indicates a successful delete cache item request. -class DeleteSuccess implements DeleteResponse {} +class DeleteSuccess extends ResponseBase implements DeleteResponse {} /// Indicates that an error occurred during the delete cache item request. /// diff --git a/lib/src/messages/responses/cache/data/scalar/get_response.dart b/lib/src/messages/responses/cache/data/scalar/get_response.dart index 75bde2b..2aecc38 100644 --- a/lib/src/messages/responses/cache/data/scalar/get_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/get_response.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:momento/src/internal/utils/display.dart'; import 'package:momento/src/messages/responses/responses_base.dart'; /// Sealed class for a get cache item request. @@ -18,7 +19,7 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class GetResponse {} /// Indicates that the requested data was not available in the cache. -class GetMiss implements GetResponse {} +class GetMiss extends ResponseBase implements GetResponse {} /// Indicates that an error occurred during the get cache item request. /// @@ -31,11 +32,16 @@ class GetError extends ErrorResponseBase implements GetResponse { } /// Indicates that the requested data was successfully retrieved from the cache and can be accessed by the fields `value` or `binaryValue`. -class GetHit implements GetResponse { +class GetHit extends ResponseBase implements GetResponse { GetHit(this._value); final List _value; String get value => utf8.decode(_value); List get binaryValue => _value; + + @override + String toString() { + return "${super.toString()}: ${truncateString(value)}"; + } } diff --git a/lib/src/messages/responses/cache/data/scalar/set_response.dart b/lib/src/messages/responses/cache/data/scalar/set_response.dart index bbcad42..1a771a9 100644 --- a/lib/src/messages/responses/cache/data/scalar/set_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/set_response.dart @@ -14,7 +14,7 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class SetResponse {} /// Indicates a successful set cache item request. -class SetSuccess implements SetResponse {} +class SetSuccess extends ResponseBase implements SetResponse {} /// Indicates that an error occurred during the set cache item request. /// diff --git a/lib/src/messages/responses/responses_base.dart b/lib/src/messages/responses/responses_base.dart index 35fda64..b5ec32f 100644 --- a/lib/src/messages/responses/responses_base.dart +++ b/lib/src/messages/responses/responses_base.dart @@ -10,3 +10,10 @@ class ErrorResponseBase extends AbstractExceptionResponseBase return "[${super.errorCode}] ${super.message}"; } } + +class ResponseBase { + @override + String toString() { + return "$runtimeType"; + } +} diff --git a/lib/src/messages/responses/topics/topic_publish.dart b/lib/src/messages/responses/topics/topic_publish.dart index 522d4c4..affcd09 100644 --- a/lib/src/messages/responses/topics/topic_publish.dart +++ b/lib/src/messages/responses/topics/topic_publish.dart @@ -2,7 +2,8 @@ import 'package:momento/src/messages/responses/responses_base.dart'; sealed class TopicPublishResponse {} -class TopicPublishSuccess implements TopicPublishResponse {} +class TopicPublishSuccess extends ResponseBase + implements TopicPublishResponse {} class TopicPublishError extends ErrorResponseBase implements TopicPublishResponse { diff --git a/lib/src/messages/responses/topics/topic_subscribe.dart b/lib/src/messages/responses/topics/topic_subscribe.dart index 36b1762..dc350df 100644 --- a/lib/src/messages/responses/topics/topic_subscribe.dart +++ b/lib/src/messages/responses/topics/topic_subscribe.dart @@ -10,7 +10,7 @@ import 'topic_subscription_item.dart'; sealed class TopicSubscribeResponse {} -class TopicSubscription implements TopicSubscribeResponse { +class TopicSubscription extends ResponseBase implements TopicSubscribeResponse { ResponseStream _stream; Int64 lastSequenceNumber; final ClientPubsub _client; diff --git a/test/src/cache/topics_test.dart b/test/src/cache/topics_test.dart index 7002e6f..8b14f70 100644 --- a/test/src/cache/topics_test.dart +++ b/test/src/cache/topics_test.dart @@ -1,4 +1,4 @@ -import 'dart:io'; +import 'dart:async'; import 'package:momento/momento.dart'; import 'package:momento/src/errors/errors.dart'; @@ -79,18 +79,24 @@ void main() { 'Expected Success but got Error: ${subscribeResp.errorCode} ${subscribeResp.message}'); } - sleep(Duration(seconds: 5)); - final publishResp = await topicClient.publish( - integrationTestCacheName, topicName, StringValue(topicValue)); - switch (publishResp) { - case TopicPublishSuccess(): - expect(publishResp.runtimeType, TopicPublishSuccess, - reason: "publish should succeed"); - case TopicPublishError(): - fail( - 'Expected Success but got Error: ${publishResp.errorCode} ${publishResp.message}'); - } - sleep(Duration(seconds: 5)); + // unsubscribe 10 seconds from now + Timer(const Duration(seconds: 10), () { + subscribeResp.unsubscribe(); + }); + + // publish a message 2 seconds from now + Timer(const Duration(seconds: 2), () async { + final publishResp = await topicClient.publish( + integrationTestCacheName, topicName, StringValue(topicValue)); + switch (publishResp) { + case TopicPublishSuccess(): + expect(publishResp.runtimeType, TopicPublishSuccess, + reason: "publish should succeed"); + case TopicPublishError(): + fail( + 'Expected Success but got Error: ${publishResp.errorCode} ${publishResp.message}'); + } + }); try { await for (final msg in subscribeResp.stream) { @@ -109,7 +115,6 @@ void main() { } subscribeResp.unsubscribe(); - topicClient.close(); }); }); }