@@ -2,6 +2,7 @@ use crate::message_routing::MessageRoutingMetrics;
2
2
3
3
use super :: * ;
4
4
use ic_base_types:: NumSeconds ;
5
+ use ic_config:: message_routing:: { MAX_STREAM_MESSAGES , TARGET_STREAM_SIZE_BYTES } ;
5
6
use ic_error_types:: RejectCode ;
6
7
use ic_management_canister_types_private:: Method ;
7
8
use ic_registry_routing_table:: { CanisterIdRange , RoutingTable } ;
@@ -336,9 +337,22 @@ fn build_streams_local_canisters() {
336
337
}
337
338
338
339
#[ test]
339
- fn build_streams_impl_at_limit_leaves_state_untouched ( ) {
340
+ fn build_streams_impl_at_message_limit_leaves_state_untouched ( ) {
341
+ build_streams_impl_at_limit_leaves_state_untouched_impl ( 0 , usize:: MAX ) ;
342
+ }
343
+
344
+ #[ test]
345
+ fn build_streams_impl_at_memory_limit_leaves_state_untouched ( ) {
346
+ build_streams_impl_at_limit_leaves_state_untouched_impl ( usize:: MAX , 0 ) ;
347
+ }
348
+
349
+ fn build_streams_impl_at_limit_leaves_state_untouched_impl (
350
+ max_stream_messages : usize ,
351
+ target_stream_size_bytes : usize ,
352
+ ) {
340
353
with_test_replica_logger ( |log| {
341
- let ( stream_builder, mut provided_state, metrics_registry) = new_fixture ( & log) ;
354
+ let ( stream_builder, mut provided_state, metrics_registry) =
355
+ new_fixture_with_limits ( & log, max_stream_messages, target_stream_size_bytes) ;
342
356
provided_state. metadata . network_topology . routing_table = Arc :: new ( RoutingTable :: try_from (
343
357
btreemap ! {
344
358
CanisterIdRange { start: CanisterId :: from( 0 ) , end: CanisterId :: from( 0xfff ) } => REMOTE_SUBNET ,
@@ -360,10 +374,7 @@ fn build_streams_impl_at_limit_leaves_state_untouched() {
360
374
let expected_state = provided_state. clone ( ) ;
361
375
362
376
// Act.
363
- let result_state = stream_builder. build_streams_impl ( provided_state. clone ( ) , usize:: MAX , 0 ) ;
364
- assert_eq ! ( result_state, expected_state) ;
365
-
366
- let result_state = stream_builder. build_streams_impl ( provided_state, 0 , usize:: MAX ) ;
377
+ let result_state = stream_builder. build_streams_impl ( provided_state. clone ( ) ) ;
367
378
assert_eq ! ( result_state, expected_state) ;
368
379
369
380
assert_eq ! (
@@ -413,18 +424,26 @@ fn build_streams_impl_respects_limits(
413
424
expected_messages : u64 ,
414
425
) {
415
426
with_test_replica_logger ( |log| {
416
- let ( stream_builder, mut provided_state, metrics_registry) = new_fixture ( & log) ;
427
+ let msgs = generate_messages_for_test ( /* senders = */ 2 , /* receivers = */ 2 ) ;
428
+ let msg_count = msgs. len ( ) ;
429
+ // All messages returned by `generate_messages_for_test` are of the same size
430
+ let msg_size = msgs. first ( ) . unwrap ( ) . count_bytes ( ) as u64 ;
431
+
432
+ // Target stream size: stream struct plus `max_stream_messages_by_byte_size - 1`
433
+ // messages plus 1 byte. Since this is a target / soft limit, it should ensure
434
+ // that exactly `max_stream_messages_by_byte_size` messages (or
435
+ // `max_stream_messages_by_byte_size * msg_size` bytes) are routed.
436
+ let target_stream_size_bytes =
437
+ size_of :: < Stream > ( ) + ( max_stream_messages_by_byte_size - 1 ) * msg_size as usize + 1 ;
438
+
439
+ let ( stream_builder, mut provided_state, metrics_registry) =
440
+ new_fixture_with_limits ( & log, max_stream_messages, target_stream_size_bytes) ;
417
441
provided_state. metadata . network_topology . routing_table = Arc :: new ( RoutingTable :: try_from (
418
442
btreemap ! {
419
443
CanisterIdRange { start: CanisterId :: from( 0 ) , end: CanisterId :: from( 0xfff ) } => REMOTE_SUBNET ,
420
444
} ,
421
445
) . unwrap ( ) ) ;
422
446
423
- let msgs = generate_messages_for_test ( /* senders = */ 2 , /* receivers = */ 2 ) ;
424
- let msg_count = msgs. len ( ) ;
425
- // All messages returned by `generate_messages_for_test` are of the same size
426
- let msg_size = msgs. first ( ) . unwrap ( ) . count_bytes ( ) as u64 ;
427
-
428
447
assert ! (
429
448
msg_count > expected_messages as usize ,
430
449
"Invalid test setup: msg_count ({}) must be greater than routed_messages ({})" ,
@@ -459,19 +478,8 @@ fn build_streams_impl_respects_limits(
459
478
streams. insert ( REMOTE_SUBNET , expected_stream) ;
460
479
} ) ;
461
480
462
- // Target stream size: stream struct plus `max_stream_messages_by_byte_size - 1`
463
- // messages plus 1 byte. Since this is a target / soft limit, it should ensure
464
- // that exactly `max_stream_messages_by_byte_size` messages (or
465
- // `max_stream_messages_by_byte_size * msg_size` bytes) are routed.
466
- let target_stream_size_bytes =
467
- size_of :: < Stream > ( ) + ( max_stream_messages_by_byte_size - 1 ) * msg_size as usize + 1 ;
468
-
469
481
// Act.
470
- let result_state = stream_builder. build_streams_impl (
471
- provided_state,
472
- max_stream_messages,
473
- target_stream_size_bytes,
474
- ) ;
482
+ let result_state = stream_builder. build_streams_impl ( provided_state) ;
475
483
476
484
assert_eq ! ( expected_state. canister_states, result_state. canister_states) ;
477
485
assert_eq ! ( expected_state. metadata, result_state. metadata) ;
@@ -998,13 +1006,19 @@ fn build_streams_with_oversized_payloads() {
998
1006
}
999
1007
1000
1008
/// Sets up the `StreamHandlerImpl`, `ReplicatedState` and `MetricsRegistry` to
1001
- /// be used by a test.
1002
- fn new_fixture ( log : & ReplicaLogger ) -> ( StreamBuilderImpl , ReplicatedState , MetricsRegistry ) {
1009
+ /// be used by a test using specific stream limits.
1010
+ fn new_fixture_with_limits (
1011
+ log : & ReplicaLogger ,
1012
+ max_stream_messages : usize ,
1013
+ target_stream_size_bytes : usize ,
1014
+ ) -> ( StreamBuilderImpl , ReplicatedState , MetricsRegistry ) {
1003
1015
let mut state = ReplicatedState :: new ( LOCAL_SUBNET , SubnetType :: Application ) ;
1004
1016
state. metadata . batch_time = Time :: from_nanos_since_unix_epoch ( 5 ) ;
1005
1017
let metrics_registry = MetricsRegistry :: new ( ) ;
1006
1018
let stream_builder = StreamBuilderImpl :: new (
1007
1019
LOCAL_SUBNET ,
1020
+ max_stream_messages,
1021
+ target_stream_size_bytes,
1008
1022
& metrics_registry,
1009
1023
& MessageRoutingMetrics :: new ( & metrics_registry) ,
1010
1024
Arc :: new ( Mutex :: new ( LatencyMetrics :: new_time_in_stream (
@@ -1017,6 +1031,12 @@ fn new_fixture(log: &ReplicaLogger) -> (StreamBuilderImpl, ReplicatedState, Metr
1017
1031
( stream_builder, state, metrics_registry)
1018
1032
}
1019
1033
1034
+ /// Sets up the `StreamHandlerImpl`, `ReplicatedState` and `MetricsRegistry` to
1035
+ /// be used by a test using default stream limits.
1036
+ fn new_fixture ( log : & ReplicaLogger ) -> ( StreamBuilderImpl , ReplicatedState , MetricsRegistry ) {
1037
+ new_fixture_with_limits ( log, MAX_STREAM_MESSAGES , TARGET_STREAM_SIZE_BYTES )
1038
+ }
1039
+
1020
1040
/// Simulates routing the given requests into a `StreamIndexedQueue` with the
1021
1041
/// given `start` index, until `byte_limit` is reached or exceeded.
1022
1042
///
0 commit comments