File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ impl Context {
5151 }
5252
5353 pub fn value ( & self , name : & str ) -> Result < Value > {
54+ // Lock is acquired and released inside `get` early to avoid contention during execution
5455 let value = self . get ( name) ;
5556 match value {
5657 Some ( ContextValue :: Variable ( v) ) => Ok ( v) ,
@@ -99,3 +100,30 @@ macro_rules! create_context {
99100 ctx
100101 } } ;
101102}
103+
104+ #[ cfg( test) ]
105+ mod tests {
106+ use crate :: context:: Context ;
107+ use crate :: value:: Value ;
108+ use std:: sync:: Arc ;
109+
110+ #[ test]
111+ fn test_context ( ) {
112+ let mut ctx = Context :: new ( ) ;
113+
114+ ctx. set_variable ( "a" , Value :: from ( 10 ) ) ;
115+ ctx. set_func ( "f" , Arc :: new ( |_| Ok ( Value :: from ( 20 ) ) ) ) ;
116+
117+ assert_eq ! ( ctx. get_variable( "a" ) . unwrap( ) , Value :: from( 10 ) ) ;
118+ assert ! ( ctx. get_variable( "f" ) . is_none( ) ) ;
119+ assert ! ( ctx. get_variable( "missing" ) . is_none( ) ) ;
120+
121+ assert ! ( ctx. get_func( "f" ) . is_some( ) ) ;
122+ assert ! ( ctx. get_func( "a" ) . is_none( ) ) ;
123+ assert ! ( ctx. get_func( "missing" ) . is_none( ) ) ;
124+
125+ assert_eq ! ( ctx. value( "a" ) . unwrap( ) , Value :: from( 10 ) ) ;
126+ assert_eq ! ( ctx. value( "f" ) . unwrap( ) , Value :: from( 20 ) ) ;
127+ assert_eq ! ( ctx. value( "missing" ) . unwrap( ) , Value :: None ) ;
128+ }
129+ }
You can’t perform that action at this time.
0 commit comments