1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use crate::{
channel::mpsc::{
UnboundedReceiver, UnboundedSender, create_unbounded_channel,
},
collections::HashMap,
global::dxb_block::{
BlockId, DXBBlock, IncomingBlockNumber, IncomingContextId,
IncomingEndpointContextId, IncomingEndpointContextSectionId,
IncomingSection, IncomingSectionIndex, OutgoingContextId,
OutgoingSectionIndex,
},
network::com_interfaces::com_interface::socket::ComInterfaceSocketUUID,
random::RandomState,
};
use crate::{disassembler::print_disassembled, prelude::*};
use core::{cell::RefCell, fmt::Debug, prelude::rust_2024::*};
use log::info;
use ringmap::RingMap;
// TODO #170: store scope memory
#[derive(Debug)]
pub struct ScopeContext {
pub next_section_index: IncomingSectionIndex,
pub next_block_number: IncomingBlockNumber,
/// timestamp of the last keep alive block
/// when a specific time has passed since the timestamp, the scope context is disposed
/// TODO #171: implement dispose of scope context
pub keep_alive_timestamp: u64,
// a reference to the sender for the current section
pub current_queue_sender: Option<UnboundedSender<DXBBlock>>,
// a cache for all blocks indexed by their block number
pub cached_blocks: BTreeMap<IncomingBlockNumber, DXBBlock>,
}
/// A scope context storing scopes of incoming DXB blocks
impl Default for ScopeContext {
fn default() -> Self {
ScopeContext {
next_section_index: 0,
next_block_number: 0,
keep_alive_timestamp: crate::time::now_ms(),
current_queue_sender: None,
cached_blocks: BTreeMap::new(),
}
}
}
// fn that gets a scope context as callback
type SectionObserver = Box<dyn FnMut(IncomingSection)>;
#[derive(Clone, Debug)]
pub struct BlockHistoryData {
/// if block originated from local endpoint, the socket uuid is None,
/// otherwise it is the uuid of the incoming socket
pub original_socket_uuid: Option<ComInterfaceSocketUUID>,
}
pub struct BlockHandler {
pub current_context_id: RefCell<OutgoingContextId>,
/// a map of active request scopes for incoming blocks
pub block_cache: RefCell<HashMap<IncomingEndpointContextId, ScopeContext>>,
/// a queue of incoming request scopes
/// the scopes can be retrieved from the request_scopes map
pub incoming_sections_sender: RefCell<UnboundedSender<IncomingSection>>,
/// a map of observers for incoming response blocks (by context_id + block_index)
/// contains an observer callback and an optional queue of blocks if the response block is a multi-block stream
pub section_observers: RefCell<
HashMap<(IncomingContextId, IncomingSectionIndex), SectionObserver>,
>,
/// history of all incoming blocks
pub incoming_blocks_history:
RefCell<RingMap<BlockId, BlockHistoryData, RandomState>>,
}
impl Debug for BlockHandler {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BlockHandler")
.field("current_context_id", &self.current_context_id)
// .field("block_cache", &self.block_cache)
.field("incoming_blocks_history", &self.incoming_blocks_history)
.finish()
}
}
const RING_MAP_CAPACITY: usize = 500;
impl BlockHandler {
pub fn init(
incoming_sections_sender: UnboundedSender<IncomingSection>,
) -> BlockHandler {
BlockHandler {
current_context_id: RefCell::new(0),
block_cache: RefCell::new(HashMap::new()),
incoming_sections_sender: RefCell::new(incoming_sections_sender),
section_observers: RefCell::new(HashMap::new()),
incoming_blocks_history: RefCell::new(
RingMap::with_capacity_and_hasher(
RING_MAP_CAPACITY,
RandomState::default(),
),
),
}
}
/// Adds a block to the history of incoming blocks
/// if the block is not already in the history
/// returns true if the block was added and not already in the history
pub fn add_block_id_to_history(
&self,
block_id: BlockId,
original_socket_uuid: Option<ComInterfaceSocketUUID>,
) {
let mut history = self.incoming_blocks_history.borrow_mut();
// only add if original block
if !history.contains_key(&block_id) {
let block_data = BlockHistoryData {
original_socket_uuid,
};
history.insert(block_id, block_data);
}
}
/// Checks if a block is already in the history
pub fn is_block_in_history(&self, block: &DXBBlock) -> bool {
let history = self.incoming_blocks_history.borrow();
let block_id = block.get_block_id();
history.contains_key(&block_id)
}
pub fn get_block_data_from_history(
&self,
block: &DXBBlock,
) -> Option<BlockHistoryData> {
let history = self.incoming_blocks_history.borrow();
let block_id = block.get_block_id();
history.get(&block_id).cloned()
}
/// Handles an incoming block by either putting it into the request queue
/// or calling the observer for the block if it is a response block
pub fn handle_incoming_block(&self, block: DXBBlock) {
info!("Handling incoming block...");
let context_id = block.block_header.context_id;
let section_index = block.block_header.section_index;
let block_number = block.block_header.block_number;
let is_response = block
.block_header
.flags_and_timestamp
.block_type()
.is_response();
info!(
"Received block (context={context_id}, section={section_index}, block_nr={block_number})"
);
print_disassembled(&block.body);
// handle observers if response block
if is_response {
self.handle_incoming_response_block(block);
} else {
self.handle_incoming_request_block(block);
}
}
// Handles incoming request blocks by putting them into the request queue
fn handle_incoming_request_block(&self, block: DXBBlock) {
let new_sections =
self.extract_complete_sections_with_new_incoming_block(block);
// put into request queue
let sender = &self.incoming_sections_sender;
for section in new_sections {
sender.borrow_mut().start_send(section).unwrap();
}
}
/// Handles incoming response blocks by calling the observer if an observer is registered
/// Returns true when the observer has consumed all blocks and should be removed
fn handle_incoming_response_block(&self, block: DXBBlock) {
let context_id = block.block_header.context_id;
let endpoint_context_id = IncomingEndpointContextId {
sender: block.routing_header.sender.clone(),
context_id,
};
let new_sections =
self.extract_complete_sections_with_new_incoming_block(block);
// try to call the observer for the incoming response block
for section in new_sections {
let section_index = section.get_section_index();
if let Some(observer) = self
.section_observers
.borrow_mut()
.get_mut(&(context_id, section_index))
{
// call the observer with the new section
observer(section);
} else {
// no observer for this scope id + block index
log::warn!(
"No observer for incoming response block (scope={endpoint_context_id:?}, block={section_index}), dropping block"
);
};
}
}
/// Takes a new incoming block and returns a vector of all new available incoming sections
/// for the block's scope
fn extract_complete_sections_with_new_incoming_block(
&self,
block: DXBBlock,
) -> Vec<IncomingSection> {
let section_index = block.block_header.section_index;
let block_number = block.block_header.block_number;
let is_end_of_section =
block.block_header.flags_and_timestamp.is_end_of_section();
let is_end_of_context =
block.block_header.flags_and_timestamp.is_end_of_context();
let endpoint_context_id = IncomingEndpointContextId {
sender: block.routing_header.sender.clone(),
context_id: block.block_header.context_id,
};
let section_context_id = IncomingEndpointContextSectionId::new(
endpoint_context_id.clone(),
section_index,
);
// get scope context if it already exists
let has_scope_context =
{ self.block_cache.borrow().contains_key(&endpoint_context_id) };
// Case 1: shortcut if no scope context exists and the block is a single block
if !has_scope_context
&& block_number == 0
&& (is_end_of_section || is_end_of_context)
{
return vec![IncomingSection::SingleBlock((
Some(block),
section_context_id.clone(),
))];
}
// make sure a scope context exists from here on
let mut request_scopes = self.block_cache.borrow_mut();
let scope_context = request_scopes
.entry(endpoint_context_id.clone())
.or_default();
// TODO #172: what happens if the endpoint has not received all blocks starting with block_number 0?
// we should still potentially process those blocks
// Case 2: if the block is the next expected block in the current section, put it into the
// section block queue and try to drain blocks from the cache
if block_number == scope_context.next_block_number {
// list of IncomingSections that is returned at the end
let mut new_blocks = vec![];
// initial values for loop variables from input block
let mut is_end_of_context = is_end_of_context;
let mut is_end_of_section = is_end_of_section;
let mut next_block = block;
let mut section_index = section_index;
// loop over the input block and potential blocks from the cache until the next block cannot be found
// or the end of the scope is reached
loop {
if let Some(sender) = &mut scope_context.current_queue_sender {
// send the next block to the section queue receiver
sender.start_send(next_block).expect(
"Failed to send block to current section queue",
);
} else {
// create a new block queue for the current section
let (mut sender, receiver) = create_unbounded_channel();
// add the first block to the queue
new_blocks.push(IncomingSection::BlockStream((
Some(receiver),
IncomingEndpointContextSectionId::new(
endpoint_context_id.clone(),
section_index,
),
)));
// send the next block to the section queue receiver
sender.start_send(next_block).expect(
"Failed to send first block to current section queue",
);
scope_context.current_queue_sender = Some(sender);
}
// cleanup / prepare for next block =======================
// increment next block number
scope_context.next_block_number += 1;
// if end of scope, remove the scope context
if is_end_of_context {
request_scopes.remove(&endpoint_context_id);
break;
}
// cleanup if section is finished
else if is_end_of_section {
// increment section index
scope_context.next_section_index += 1;
// close and remove the current section queue sender
if let Some(sender) =
scope_context.current_queue_sender.take()
{
sender.close_channel();
}
}
// ========================================================
// check if next block is in cache for next iteration
if let Some(next_cached_block) = scope_context
.cached_blocks
.remove(&scope_context.next_block_number)
{
// check if block is end of section
is_end_of_section = next_cached_block
.block_header
.flags_and_timestamp
.is_end_of_section();
// check if block is end of scope
is_end_of_context = next_cached_block
.block_header
.flags_and_timestamp
.is_end_of_context();
// set next block
next_block = next_cached_block;
// update section index from next block
section_index = next_block.block_header.section_index;
}
// no more blocks in cache, break
else {
break;
}
}
new_blocks
}
// Case 3: if the block is not the next expected block in the current section,
// put it into the block cache
else {
// check if block is already in cache
// TODO #173: this should not happen, we should make sure duplicate blocks are dropped before
if scope_context.cached_blocks.contains_key(&block_number) {
log::warn!(
"Block {block_number} already in cache, dropping block"
);
}
// add block to cache
scope_context.cached_blocks.insert(block_number, block);
vec![]
}
}
pub fn get_new_context_id(&self) -> OutgoingContextId {
*self.current_context_id.borrow_mut() += 1;
*self.current_context_id.borrow()
}
/// Adds a new observer for incoming blocks with a specific scope id and block index
/// Returns a receiver that can be awaited to get the incoming sections
pub fn register_incoming_block_observer(
&self,
context_id: OutgoingContextId,
section_index: OutgoingSectionIndex,
) -> UnboundedReceiver<IncomingSection> {
let (tx, rx) = create_unbounded_channel::<IncomingSection>();
let tx = Rc::new(RefCell::new(tx));
// create observer callback for scope id + block index
let observer = move |blocks: IncomingSection| {
tx.clone().borrow_mut().start_send(blocks).unwrap();
};
// add new scope observer
self.section_observers
.borrow_mut()
.insert((context_id, section_index), Box::new(observer));
rx
}
/// Waits for incoming response block with a specific scope id and block index
pub async fn wait_for_incoming_response_block(
&self,
context_id: OutgoingContextId,
section_index: OutgoingSectionIndex,
) -> Option<IncomingSection> {
let _rx =
self.register_incoming_block_observer(context_id, section_index);
// Await the result from the callback
// FIXME #174
None
// rx.next().await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
global::{
dxb_block::{DXBBlock, IncomingSection},
protocol_structures::{
block_header::{BlockHeader, BlockType, FlagsAndTimestamp},
routing_header::{Receivers, RoutingHeader},
},
},
values::core_values::endpoint::Endpoint,
};
use core::str::FromStr;
use ntest_timeout::timeout;
lazy_static::lazy_static! {
pub static ref TEST_ENDPOINT_ORIGIN: Endpoint = Endpoint::from_str("@origin").unwrap();
pub static ref TEST_ENDPOINT_A: Endpoint = Endpoint::from_str("@test-a").unwrap();
pub static ref TEST_ENDPOINT_B: Endpoint = Endpoint::from_str("@test-b").unwrap();
}
#[tokio::test]
async fn receive_single_block() {
let (incoming_sections_sender, mut incoming_sections_receiver) =
create_unbounded_channel::<IncomingSection>();
let block_handler = BlockHandler::init(incoming_sections_sender);
let context_id = block_handler.get_new_context_id();
// Create a single DXB block
let mut block = DXBBlock {
block_header: BlockHeader {
context_id,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(true)
.with_is_end_of_context(true),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
let block_endpoint_context_id = block.get_endpoint_context_id();
// Send incoming block to block handler
block_handler.handle_incoming_block(block);
// block must be in incoming_sections_receiver
let incoming_block = incoming_sections_receiver.next().await.unwrap();
match &incoming_block {
IncomingSection::SingleBlock((Some(received_block), ..)) => {
assert_eq!(
received_block.get_endpoint_context_id(),
block_endpoint_context_id
);
}
_ => panic!("Expected a SingleBlock section"),
}
}
#[tokio::test]
async fn receive_multiple_blocks() {
let (incoming_sections_sender, mut incoming_sections_receiver) =
create_unbounded_channel::<IncomingSection>();
let block_handler = BlockHandler::init(incoming_sections_sender);
let context_id = block_handler.get_new_context_id();
let section_index = 42;
// Create multiple DXB blocks for the same context and section
let mut blocks = gen move {
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index,
block_number: 0,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(false)
.with_is_end_of_context(false),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index,
block_number: 1,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(true)
.with_is_end_of_context(true),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
};
// 1. Send first block
block_handler.handle_incoming_block(blocks.next().unwrap());
info!("Checking incoming sections...");
// block must be in incoming_sections_queue
let mut section = incoming_sections_receiver.next().await.unwrap();
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index
);
// blocks queue must contain the first block
assert!(section.next().await.is_some());
}
_ => core::panic!("Expected a BlockStream section"),
}
// 2. Send second block
block_handler.handle_incoming_block(blocks.next().unwrap());
// no new incoming sections, old section receives new blocks
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index
);
// blocks queue length must be 2 (was not yet drained)
assert_eq!(section.drain().await.len(), 1);
}
_ => core::panic!("Expected a BlockStream section"),
}
}
#[tokio::test]
async fn receive_multiple_blocks_wrong_order() {
let (incoming_sections_sender, mut incoming_sections_receiver) =
create_unbounded_channel::<IncomingSection>();
let block_handler = BlockHandler::init(incoming_sections_sender);
let context_id = block_handler.get_new_context_id();
let section_index = 42;
// Create multiple DXB blocks for the same context and section in wrong order
let mut blocks = gen move {
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index,
block_number: 1,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(true)
.with_is_end_of_context(true),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index,
block_number: 0,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(false)
.with_is_end_of_context(false),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
};
// 1. Send first block
block_handler.handle_incoming_block(blocks.next().unwrap());
// 2. Send second block
block_handler.handle_incoming_block(blocks.next().unwrap());
// block must be in incoming_sections_queue
let mut section = incoming_sections_receiver.next().await.unwrap();
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index.clone(),
section_index
);
// blocks queue length must be 2
let blocks = section.drain().await;
assert_eq!(blocks.len(), 2);
// check order:
// first block must have block number 0
let block = blocks.first().unwrap();
assert_eq!(block.block_header.block_number, 0);
// second block must have block number 1
let block = blocks.get(1).unwrap();
assert_eq!(block.block_header.block_number, 1);
}
_ => core::panic!("Expected a BlockStream section"),
}
}
#[tokio::test]
async fn receive_multiple_sections() {
let (incoming_sections_sender, mut incoming_sections_receiver) =
create_unbounded_channel::<IncomingSection>();
let block_handler = BlockHandler::init(incoming_sections_sender);
let context_id = block_handler.get_new_context_id();
let section_index_1 = 42;
let section_index_2 = 43;
// Create multiple DXB blocks for two sections
let mut blocks = gen move {
// first section
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index: section_index_1,
block_number: 0,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(false)
.with_is_end_of_context(false),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index: section_index_1,
block_number: 1,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(true)
.with_is_end_of_context(false),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
// second section, end of context
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index: section_index_2,
block_number: 2,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(false)
.with_is_end_of_context(false),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
yield DXBBlock {
block_header: BlockHeader {
context_id,
section_index: section_index_2,
block_number: 3,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_is_end_of_section(true)
.with_is_end_of_context(true),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
};
// 1. Send first block
block_handler.handle_incoming_block(blocks.next().unwrap());
// block must be in incoming_sections_queue
let mut section = incoming_sections_receiver.next().await.unwrap();
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index_1
);
// block queue must contain the first block
assert!(section.next().await.is_some());
}
_ => core::panic!("Expected a BlockStream section"),
}
// 2. Send second block
block_handler.handle_incoming_block(blocks.next().unwrap());
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index_1
);
// blocks queue length must be 1
assert_eq!(section.drain().await.len(), 1);
}
_ => core::panic!("Expected a BlockStream section"),
}
// 3. Send third block
block_handler.handle_incoming_block(blocks.next().unwrap());
// block must be in incoming_sections_queue
let mut section = incoming_sections_receiver.next().await.unwrap();
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index_2
);
// block queue must contain the first block
assert!(section.next().await.is_some());
}
_ => core::panic!("Expected a BlockStream section"),
}
// 4. Send fourth block
block_handler.handle_incoming_block(blocks.next().unwrap());
// block must not be in incoming_sections_queue
// block must be a block stream
match §ion {
IncomingSection::BlockStream((
Some(blocks),
incoming_context_section_id,
)) => {
// section must match
assert_eq!(
incoming_context_section_id.section_index,
section_index_2
);
// blocks queue length must be 1
assert_eq!(section.drain().await.len(), 1);
}
_ => core::panic!("Expected a BlockStream section"),
}
}
#[tokio::test]
#[timeout(2000)]
#[cfg(feature = "std")]
async fn await_response_block() {
let (incoming_sections_sender, mut incoming_sections_receiver) =
create_unbounded_channel::<IncomingSection>();
let block_handler = BlockHandler::init(incoming_sections_sender);
let context_id = block_handler.get_new_context_id();
let section_index = 42;
// Create a single DXB block
let block = DXBBlock {
block_header: BlockHeader {
context_id,
section_index,
flags_and_timestamp: FlagsAndTimestamp::new()
.with_block_type(BlockType::Response)
.with_is_end_of_section(true)
.with_is_end_of_context(true),
..BlockHeader::default()
},
routing_header: RoutingHeader::default()
.with_sender(TEST_ENDPOINT_A.clone())
.with_receivers(Receivers::Endpoints(vec![
TEST_ENDPOINT_ORIGIN.clone(),
]))
.to_owned(),
..DXBBlock::default()
};
// set observer for the block
let mut rx = block_handler
.register_incoming_block_observer(context_id, section_index);
// Put into incoming queue of mock interface
block_handler.handle_incoming_block(block);
// await receiver
let response = rx.next().await.unwrap();
// IncomingSection must be a SingleBlock
match response {
IncomingSection::SingleBlock((Some(block), _)) => {
info!("section: {block:?}");
assert_eq!(block.block_header.context_id, context_id);
assert_eq!(block.block_header.section_index, section_index);
}
_ => core::panic!("Expected a SingleBlock section"),
}
}
}