@@ -5,6 +5,7 @@ namespace TestStack.BDDfy
55 public class TestContext : ITestContext
66 {
77 private static readonly Dictionary < object , ITestContext > ContextLookup = new Dictionary < object , ITestContext > ( ) ;
8+ private static object _dictionaryLock = new object ( ) ;
89
910 private TestContext ( object testObject )
1011 {
@@ -17,15 +18,18 @@ public static void SetContext(object testObject, ITestContext context)
1718 var fluentBuilder = testObject as IFluentStepBuilder ;
1819 if ( fluentBuilder != null ) testObject = fluentBuilder . TestObject ;
1920
20- if ( ContextLookup . ContainsKey ( testObject ) )
21+ lock ( _dictionaryLock )
2122 {
22- var oldContext = ContextLookup [ testObject ] ;
23- context . Examples = oldContext . Examples ;
24- ContextLookup [ testObject ] = new TestContext ( testObject ) ;
25- }
26- else
27- {
28- ContextLookup . Add ( testObject , context ) ;
23+ if ( ContextLookup . ContainsKey ( testObject ) )
24+ {
25+ var oldContext = ContextLookup [ testObject ] ;
26+ context . Examples = oldContext . Examples ;
27+ ContextLookup [ testObject ] = new TestContext ( testObject ) ;
28+ }
29+ else
30+ {
31+ ContextLookup . Add ( testObject , context ) ;
32+ }
2933 }
3034 }
3135
@@ -34,16 +38,22 @@ public static ITestContext GetContext(object testObject)
3438 var fluentBuilder = testObject as IFluentStepBuilder ;
3539 if ( fluentBuilder != null ) testObject = fluentBuilder . TestObject ;
3640
37- if ( ! ContextLookup . ContainsKey ( testObject ) )
38- ContextLookup . Add ( testObject , new TestContext ( testObject ) ) ;
41+ lock ( _dictionaryLock )
42+ {
43+ if ( ! ContextLookup . ContainsKey ( testObject ) )
44+ ContextLookup . Add ( testObject , new TestContext ( testObject ) ) ;
3945
40- return ContextLookup [ testObject ] ;
46+ return ContextLookup [ testObject ] ;
47+ }
4148 }
4249
4350 public static void ClearContextFor ( object testObject )
4451 {
45- if ( ContextLookup . ContainsKey ( testObject ) )
46- ContextLookup . Remove ( testObject ) ;
52+ lock ( _dictionaryLock )
53+ {
54+ if ( ContextLookup . ContainsKey ( testObject ) )
55+ ContextLookup . Remove ( testObject ) ;
56+ }
4757 }
4858
4959 public ExampleTable Examples { get ; set ; }
0 commit comments