fn execute_instructions()

in quic/s2n-quic-transport/src/stream/send_stream/tests.rs [126:257]


fn execute_instructions(test_env: &mut TestEnvironment, instructions: &[Instruction]) {
    println!("executing {} instructions", instructions.len());
    for (id, instruction) in instructions.iter().enumerate() {
        println!("Executing instruction {:?} {:?}", id, instruction);
        match instruction {
            Instruction::EnqueueData(offset, size, expect_success) => {
                let data = Bytes::from(gen_pattern_test_data(*offset, *size));

                let poll_result = test_env.poll_push(data);

                if *expect_success {
                    assert_eq!(poll_result, Poll::Ready(Ok(())));
                } else {
                    assert_eq!(poll_result, Poll::Pending);
                }
            }
            Instruction::Finish(expect_success) => {
                let poll_result = test_env.poll_finish();

                if *expect_success {
                    assert_eq!(poll_result, Poll::Ready(Ok(())));
                } else {
                    assert_eq!(poll_result, Poll::Pending);
                }
            }
            Instruction::Reset(error_code, expect_success) => {
                let result = test_env
                    .stream
                    .poll_request(ops::Request::default().reset(*error_code), None);

                assert_eq!(*expect_success, result.is_ok(), "Unexpected reset result");
            }
            Instruction::SetMaxData(max_data) => {
                let was_waiting_for_connection_window = test_env
                    .stream
                    .get_stream_interests()
                    .connection_flow_control_credits;
                test_env.tx_connection_flow_controller.on_max_data(MaxData {
                    maximum_data: *max_data,
                });

                if was_waiting_for_connection_window {
                    test_env.stream.on_connection_window_available();
                }
            }
            Instruction::SetMaxStreamData(max_stream_data, expect_writer_wakeup) => {
                let mut events = StreamEvents::new();
                assert!(test_env
                    .stream
                    .on_max_stream_data(
                        &MaxStreamData {
                            stream_id: test_env.stream.stream_id.into(),
                            maximum_stream_data: *max_stream_data,
                        },
                        &mut events
                    )
                    .is_ok());
                assert!(events.read_wake.is_none());
                if let ExpectWakeup(Some(wakeup_expected)) = expect_writer_wakeup {
                    assert_eq!(*wakeup_expected, events.write_wake.is_some());
                }
            }
            Instruction::StopSending(error_code, expect_writer_wakeup) => {
                let mut events = StreamEvents::new();
                assert!(test_env
                    .stream
                    .on_stop_sending(
                        &StopSending {
                            stream_id: test_env.stream.stream_id.into(),
                            application_error_code: (*error_code).into(),
                        },
                        &mut events,
                    )
                    .is_ok());
                if let ExpectWakeup(Some(wakeup_expected)) = expect_writer_wakeup {
                    assert_eq!(*wakeup_expected, events.write_wake.is_some());
                }
            }
            Instruction::CheckNoTx => {
                test_env.sent_frames.clear();
                test_env.assert_write_frames(0);
                assert_eq!(
                    test_env.sent_frames.len(),
                    0,
                    "Expected no queued frames {:?}",
                    &test_env.sent_frames
                );
            }
            Instruction::CheckDataTx(
                expected_offset,
                expected_size,
                expect_eof,
                expect_is_last_frame,
                expected_packet_number,
            ) => {
                test_env.assert_write_of(
                    *expected_offset,
                    *expected_size,
                    *expect_eof,
                    *expect_is_last_frame,
                    *expected_packet_number,
                );
            }
            Instruction::CheckStreamDataBlockedTx(stream_data_limit, expected_packet_number) => {
                test_env.assert_write_stream_data_blocked_frame(
                    *stream_data_limit,
                    *expected_packet_number,
                )
            }
            Instruction::CheckResetTx(
                expected_error_code,
                expected_packet_number,
                expected_final_size,
            ) => {
                test_env.assert_write_reset_frame(
                    *expected_error_code,
                    *expected_packet_number,
                    *expected_final_size,
                );
            }
            Instruction::CheckInterests(expected_interests) => {
                assert_eq!(*expected_interests, test_env.stream.get_stream_interests());
            }
            Instruction::AckPacket(packet_number, expect_writer_wakeup) => {
                test_env.ack_packet(*packet_number, *expect_writer_wakeup);
            }
            Instruction::NackPacket(packet_number) => {
                test_env.nack_packet(*packet_number);
            }
        }
    }
}