|
| 1 | +use std::sync::mpsc::sync_channel; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use rstest::rstest; |
| 5 | + |
| 6 | +use crate::blockifier::config::CairoNativeRunConfig; |
| 7 | +use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass}; |
| 8 | +use crate::state::contract_class_manager::{CompilationRequest, ContractClassManager}; |
| 9 | +use crate::state::global_cache::{ContractCaches, GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST}; |
| 10 | +use crate::test_utils::contracts::FeatureContract; |
| 11 | +use crate::test_utils::{CairoVersion, RunnableCairo1}; |
| 12 | + |
| 13 | +const TEST_CHANNEL_SIZE: usize = 10; |
| 14 | + |
| 15 | +fn get_casm(test_contract: FeatureContract) -> CompiledClassV1 { |
| 16 | + match test_contract.get_runnable_class() { |
| 17 | + RunnableCompiledClass::V1(casm) => casm, |
| 18 | + _ => panic!("Expected CompiledClassV1"), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn create_test_request_from_contract(test_contract: FeatureContract) -> CompilationRequest { |
| 23 | + let class_hash = test_contract.get_class_hash(); |
| 24 | + let sierra = Arc::new(test_contract.get_sierra()); |
| 25 | + let casm = get_casm(test_contract); |
| 26 | + |
| 27 | + (class_hash, sierra, casm) |
| 28 | +} |
| 29 | + |
| 30 | +fn create_test_request() -> CompilationRequest { |
| 31 | + let test_contract = FeatureContract::TestContract(CairoVersion::Cairo1(RunnableCairo1::Casm)); |
| 32 | + create_test_request_from_contract(test_contract) |
| 33 | +} |
| 34 | + |
| 35 | +#[rstest] |
| 36 | +#[case::run_native_while_waiting(true, true)] |
| 37 | +#[case::run_native_without_waiting(true, false)] |
| 38 | +#[case::run_without_native(false, false)] |
| 39 | +fn test_start(#[case] run_cairo_native: bool, #[case] wait_on_native_compilation: bool) { |
| 40 | + let native_config = |
| 41 | + CairoNativeRunConfig { run_cairo_native, wait_on_native_compilation, ..Default::default() }; |
| 42 | + let manager = ContractClassManager::create_for_testing(native_config); |
| 43 | + |
| 44 | + assert_eq!(manager.cairo_native_run_config, native_config); |
| 45 | + if run_cairo_native { |
| 46 | + if wait_on_native_compilation { |
| 47 | + assert!( |
| 48 | + manager.sender.is_none(), |
| 49 | + "Sender should be None - the compilation worker is not used." |
| 50 | + ); |
| 51 | + assert!( |
| 52 | + manager.compiler.is_some(), |
| 53 | + "Compiler should be Some - compilation is not offloaded to the compilation worker." |
| 54 | + ); |
| 55 | + } else { |
| 56 | + assert!( |
| 57 | + manager.sender.is_some(), |
| 58 | + "Sender should be Some - the compilation worker is used." |
| 59 | + ); |
| 60 | + assert!( |
| 61 | + manager.compiler.is_none(), |
| 62 | + "Compiler should be None - compilation is offloaded to the compilation worker." |
| 63 | + ); |
| 64 | + } |
| 65 | + } else { |
| 66 | + assert!(manager.sender.is_none(), "Sender should be None- Cairo native is disabled."); |
| 67 | + assert!(manager.compiler.is_none(), "Compiler should be None - Cairo native is disabled."); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +#[should_panic(expected = "Compilation request channel is closed.")] |
| 73 | +fn test_send_compilation_request_channel_disconnected() { |
| 74 | + // We use the channel to send native compilation requests. |
| 75 | + let native_config = CairoNativeRunConfig { |
| 76 | + run_cairo_native: true, |
| 77 | + wait_on_native_compilation: false, |
| 78 | + channel_size: TEST_CHANNEL_SIZE, |
| 79 | + }; |
| 80 | + let (sender, receiver) = sync_channel(native_config.channel_size); |
| 81 | + let manager = ContractClassManager { |
| 82 | + cairo_native_run_config: native_config, |
| 83 | + contract_caches: ContractCaches::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST), |
| 84 | + sender: Some(sender), |
| 85 | + compiler: None, |
| 86 | + }; |
| 87 | + // Disconnect the channel by dropping the receiver. |
| 88 | + drop(receiver); |
| 89 | + |
| 90 | + // Sending request with a disconnected channel should panic. |
| 91 | + let request = create_test_request(); |
| 92 | + manager.send_compilation_request(request); |
| 93 | +} |
0 commit comments