Skip to content

Commit

Permalink
Simplify fiber yield testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rosik authored and gmoshkin committed Nov 1, 2021
1 parent bea368e commit efaaa26
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions tests/src/test_fiber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{
use tarantool::fiber::{
self, fiber_yield, is_cancelled, sleep, Cond, Fiber, FiberAttr
};
use tarantool::{space, transaction, error::{Error, TransactionError}};
use tarantool::hlua::{
Lua,
LuaFunction
};

pub fn test_fiber_new() {
let mut fiber = Fiber::new("test_fiber", &mut |_| 0);
Expand Down Expand Up @@ -329,40 +332,43 @@ pub fn test_multiple_unit_deferred() {
assert_eq!(res, vec![1, 2, 3, 4, 5, 6, 7, 8]);
}

pub fn immediate_yields() {
let mut space = space::Space::find("test_s1").unwrap();
space.truncate().unwrap();

let mut fib = None;
fn fiber_csw() -> i32 {
static mut FLAG: bool = false;
let mut lua: Lua = crate::hlua::global();

if unsafe { !FLAG } {
lua.execute::<()>(r#"
function fiber_csw()
local fiber = require('fiber')
return fiber.info()[fiber.id()].csw
end
"#).unwrap();
unsafe { FLAG = true; }
}

let result = transaction::start_transaction(|| -> Result<(), Error> {
space.insert(&(1, "test".to_string()))?;
fib = Some(fiber::start(|| 69));
Ok(())
});
return lua.get::<LuaFunction<_>, _>("fiber_csw").unwrap().call().unwrap();
}

assert!(matches!(
result,
Err(Error::Transaction(TransactionError::FailedToCommit)),
));
pub fn immediate_yields() {
let mut upvalue = 0;
let csw1 = fiber_csw();
fiber::start(|| upvalue = 69);
let csw2 = fiber_csw();

assert_eq!(fib.map(|f| f.join()), Some(69))
assert_eq!(upvalue, 69);
assert_eq!(csw2, csw1+1);
}

pub fn deferred_doesnt_yield() {
let mut space = space::Space::find("test_s1").unwrap();
space.truncate().unwrap();

let mut fib = None;

let result = transaction::start_transaction(|| -> Result<(), Error> {
space.insert(&(1, "test".to_string()))?;
fib = Some(fiber::defer(|| 69));
Ok(())
});
let mut upvalue = 0;
let csw1 = fiber_csw();
fiber::defer(|| upvalue = 96);
let csw2 = fiber_csw();

assert!(result.is_ok());
assert_eq!(upvalue, 0);
assert_eq!(csw2, csw1);

assert!(matches!(fib.map(|f| f.join()), Some(Ok(69))))
fiber::sleep(0.);
assert_eq!(upvalue, 96);
}

0 comments on commit efaaa26

Please sign in to comment.