File tree Expand file tree Collapse file tree 2 files changed +75
-7
lines changed
src/WebJobs.Script/Config
test/WebJobs.Script.Tests/Config Expand file tree Collapse file tree 2 files changed +75
-7
lines changed Original file line number Diff line number Diff line change 3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Linq ;
6
+ using System . Threading ;
7
7
8
8
namespace Microsoft . Azure . WebJobs . Script . Config
9
9
{
10
- public class ScriptTypeLocator : ITypeLocator
10
+ public class ScriptTypeLocator : ITypeLocator , IDisposable
11
11
{
12
+ private readonly ManualResetEventSlim _typesSetEvent ;
13
+ private readonly TimeSpan _setWaitTimeout ;
12
14
private Type [ ] _types ;
15
+ private bool _disposed ;
13
16
14
17
public ScriptTypeLocator ( )
18
+ : this ( TimeSpan . FromMinutes ( 2 ) )
19
+ { }
20
+
21
+ internal ScriptTypeLocator ( TimeSpan setWaitTimeout )
15
22
{
16
- _types = Array . Empty < Type > ( ) ;
23
+ _typesSetEvent = new ManualResetEventSlim ( false ) ;
24
+ _setWaitTimeout = setWaitTimeout ;
17
25
}
18
26
19
27
public IReadOnlyList < Type > GetTypes ( )
20
28
{
29
+ if ( ! _typesSetEvent . Wait ( _setWaitTimeout ) )
30
+ {
31
+ throw new TimeoutException ( $ "Timeout waiting for types to be set in { nameof ( ScriptTypeLocator ) } .") ;
32
+ }
33
+
21
34
return _types ;
22
35
}
23
36
24
37
internal void SetTypes ( IEnumerable < Type > types )
25
38
{
26
- if ( types == null )
39
+ ArgumentNullException . ThrowIfNull ( types ) ;
40
+
41
+ _types = [ .. types ] ;
42
+ _typesSetEvent . Set ( ) ;
43
+ }
44
+
45
+ public void Dispose ( ) => Dispose ( true ) ;
46
+
47
+ protected virtual void Dispose ( bool disposing )
48
+ {
49
+ if ( ! _disposed )
27
50
{
28
- throw new ArgumentNullException ( nameof ( types ) ) ;
29
- }
51
+ if ( disposing )
52
+ {
53
+ _typesSetEvent . Dispose ( ) ;
54
+ }
30
55
31
- _types = types . ToArray ( ) ;
56
+ _disposed = true ;
57
+ }
32
58
}
33
59
}
34
60
}
Original file line number Diff line number Diff line change
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Threading . Tasks ;
6
+ using Xunit ;
7
+
8
+ namespace Microsoft . Azure . WebJobs . Script . Config . Tests
9
+ {
10
+ public class ScriptTypeLocatorTests
11
+ {
12
+ [ Fact ]
13
+ public void GetTypes_ThrowsTimeoutException_WhenSetTypesNotCalled ( )
14
+ {
15
+ var locator = new ScriptTypeLocator ( TimeSpan . FromSeconds ( 3 ) ) ;
16
+
17
+ Assert . Throws < TimeoutException > ( ( ) => locator . GetTypes ( ) ) ;
18
+ }
19
+
20
+ [ Fact ]
21
+ public void GetTypes_ReturnsTypes_WhenSetTypesCalled ( )
22
+ {
23
+ var locator = new ScriptTypeLocator ( ) ;
24
+ var expectedTypes = new [ ] { typeof ( string ) , typeof ( int ) } ;
25
+
26
+ Task . Run ( ( ) => locator . SetTypes ( expectedTypes ) ) ;
27
+ var types = locator . GetTypes ( ) ;
28
+
29
+ Assert . Equal ( expectedTypes , types ) ;
30
+ }
31
+
32
+ [ Fact ]
33
+ public void Dispose_DisposesManualResetEventSlim ( )
34
+ {
35
+ var locator = new ScriptTypeLocator ( ) ;
36
+
37
+ locator . Dispose ( ) ;
38
+
39
+ Assert . Throws < ObjectDisposedException > ( ( ) => locator . GetTypes ( ) ) ;
40
+ }
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments