MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / bytes_stream_producer

Function bytes_stream_producer

tests/all/component_model/async.rs:919–972  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

917#[tokio::test]
918#[cfg_attr(miri, ignore)]
919async fn bytes_stream_producer() -> Result<()> {
920 let mut config = Config::new();
921 config.wasm_component_model_async(true);
922 let engine = Engine::new(&config)?;
923
924 let component = Component::new(
925 &engine,
926 r#"
927 (component
928 (core module $libc (memory (export "mem") 1))
929 (core instance $libc (instantiate $libc))
930 (core module $m
931 (import "" "mem" (memory 1))
932 (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))
933
934 (func (export "read") (param i32 i32) (result i32)
935 (call $stream.read (local.get 0) (i32.const 0) (local.get 1))
936 )
937 )
938 (type $s (stream u8))
939 (core func $stream.read (canon stream.read $s async (memory $libc "mem")))
940 (core instance $i (instantiate $m
941 (with "" (instance
942 (export "mem" (memory $libc "mem"))
943 (export "stream.read" (func $stream.read))
944 ))
945 ))
946 (func (export "read") (param "s" (stream u8)) (param "l" u32) (result u32)
947 (canon lift (core func $i "read")))
948 )
949 "#,
950 )?;
951
952 let linker = Linker::new(&engine);
953 let mut store = Store::new(&engine, ());
954 let instance = linker.instantiate_async(&mut store, &component).await?;
955 let func = instance.get_typed_func::<(StreamReader<u8>, u32), (u32,)>(&mut store, "read")?;
956
957 // read less than the capacity
958 let reader = StreamReader::new(&mut store, bytes::Bytes::from_static(b"hello"))?;
959 assert_eq!(
960 func.call_async(&mut store, (reader, 1)).await?,
961 ((1 << 4) | 0,),
962 );
963
964 // read more than the capacity
965 let reader = StreamReader::new(&mut store, bytes::Bytes::from_static(b"hello"))?;
966 assert_eq!(
967 func.call_async(&mut store, (reader, 100)).await?,
968 ((5 << 4) | 1,),
969 );
970
971 Ok(())
972}
973
974#[tokio::test]
975#[cfg_attr(miri, ignore)]

Callers

nothing calls this directly

Calls 4

OkFunction · 0.85
newFunction · 0.50
instantiate_asyncMethod · 0.45

Tested by

no test coverage detected