Skip to content

Commit 74e8c74

Browse files
committed
Throw an exception when GraphSession already exists.
1 parent 18f39c5 commit 74e8c74

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,44 @@ public class GraphSessionTests
88
[Fact]
99
public void GraphSessionShouldBeInitilizedAfterInitializerIsCalled()
1010
{
11-
GraphSessionInitializer.InitializeSession();
11+
GraphSession.Initialize(() => new GraphSession());
1212

1313
Assert.NotNull(GraphSession.Instance);
1414
Assert.Null(GraphSession.Instance.AuthContext);
15+
16+
// reset static instance.
17+
GraphSession.Reset();
1518
}
1619

1720
[Fact]
1821
public void ShouldOverwriteExistingGraphSession()
1922
{
20-
GraphSessionInitializer.InitializeSession();
23+
GraphSession.Initialize(() => new GraphSession());
2124
Guid originalSessionId = GraphSession.Instance._graphSessionId;
2225

2326
GraphSession.Initialize(() => new GraphSession(), true);
2427

2528
Assert.NotNull(GraphSession.Instance);
26-
Assert.NotEqual(GraphSession.Instance._graphSessionId, originalSessionId);
29+
Assert.NotEqual(originalSessionId, GraphSession.Instance._graphSessionId);
30+
31+
// reset static instance.
32+
GraphSession.Reset();
2733
}
2834

2935
[Fact]
3036
public void ShouldNotOverwriteExistingGraphSession()
3137
{
32-
GraphSessionInitializer.InitializeSession();
38+
GraphSession.Initialize(() => new GraphSession());
3339
Guid originalSessionId = GraphSession.Instance._graphSessionId;
3440

35-
GraphSession.Initialize(() => new GraphSession());
41+
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(() => GraphSession.Initialize(() => new GraphSession()));
3642

43+
Assert.Equal("An instance of GraphSession already exists. Call Initialize(Func<GraphSession>, bool) to overwrite it.", exception.Message);
3744
Assert.NotNull(GraphSession.Instance);
38-
Assert.Equal(GraphSession.Instance._graphSessionId, originalSessionId);
45+
Assert.Equal(originalSessionId, GraphSession.Instance._graphSessionId);
46+
47+
// reset static instance.
48+
GraphSession.Reset();
3949
}
4050
}
4151
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Xunit;
2+
3+
[assembly: CollectionBehavior(DisableTestParallelization = true)]

src/Authentication/Authentication/Common/GraphSession.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class GraphSession : IGraphSession
2020
/// Gets or Sets <see cref="IAuthContext"/>.
2121
/// </summary>
2222
public IAuthContext AuthContext { get; set; }
23+
24+
/// <summary>
25+
/// Gets an instance of <see cref="GraphSession"/>.
26+
/// </summary>
2327
public static GraphSession Instance
2428
{
2529
get
@@ -50,6 +54,7 @@ public static GraphSession Instance
5054
}
5155
}
5256
}
57+
5358
/// <summary>
5459
/// Creates a new GraphSession.
5560
/// </summary>
@@ -75,6 +80,10 @@ public static void Initialize(Func<GraphSession> instanceCreator, bool overwrite
7580
_instance = instanceCreator();
7681
_initialized = true;
7782
}
83+
else
84+
{
85+
throw new InvalidOperationException(string.Format(ErrorConstants.Message.InstanceExists, nameof(GraphSession), "Initialize(Func<GraphSession>, bool)"));
86+
}
7887
}
7988
finally
8089
{
@@ -127,5 +136,33 @@ public static void Modify(Action<GraphSession> modifier)
127136
throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException);
128137
}
129138
}
139+
140+
/// <summary>
141+
/// Resets the current instance of <see cref="GraphSession"/> to initial state.
142+
/// </summary>
143+
internal static void Reset()
144+
{
145+
try
146+
{
147+
sessionLock.EnterWriteLock();
148+
try
149+
{
150+
_instance = null;
151+
_initialized = false;
152+
}
153+
finally
154+
{
155+
sessionLock.ExitWriteLock();
156+
}
157+
}
158+
catch (LockRecursionException lockException)
159+
{
160+
throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteRecursion, lockException);
161+
}
162+
catch (ObjectDisposedException disposedException)
163+
{
164+
throw new InvalidOperationException(ErrorConstants.Codes.SessionLockWriteDisposed, disposedException);
165+
}
166+
}
130167
}
131168
}

src/Authentication/Authentication/ErrorConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal static class Message
1919
{
2020
internal const string InvalidJWT = "Invalid JWT access token.";
2121
internal const string MissingAuthContext = "Authentication needed, call Connect-Graph.";
22+
internal const string InstanceExists = "An instance of {0} already exists. Call {1} to overwrite it.";
2223
}
2324
}
2425
}

0 commit comments

Comments
 (0)