From d1a82f1363cbc4146b5ec7cf8ba37f61448e8bfa Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Wed, 15 Jul 2020 16:23:07 -0700 Subject: [PATCH 1/2] Make GraphSession thread safe. --- .../Helpers/GraphSessionTests.cs | 14 ++++++++++++-- .../Authentication/Common/GraphSession.cs | 4 ---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs b/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs index 6f165d9d568..2bb665e8ee8 100644 --- a/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs +++ b/src/Authentication/Authentication.Test/Helpers/GraphSessionTests.cs @@ -38,14 +38,24 @@ public void ShouldNotOverwriteExistingGraphSession() GraphSession.Initialize(() => new GraphSession()); Guid originalSessionId = GraphSession.Instance._graphSessionId; - InvalidOperationException exception = Assert.Throws(() => GraphSession.Initialize(() => new GraphSession())); + GraphSession.Initialize(() => new GraphSession()); - Assert.Equal("An instance of GraphSession already exists. Call Initialize(Func, bool) to overwrite it.", exception.Message); Assert.NotNull(GraphSession.Instance); Assert.Equal(originalSessionId, GraphSession.Instance._graphSessionId); // reset static instance. GraphSession.Reset(); } + + [Fact] + public void ShouldThrowExceptionWhenSessionIsNotInitialized() + { + InvalidOperationException exception = Assert.Throws(() => GraphSession.Instance); + + Assert.Equal(ErrorConstants.Codes.SessionNotInitialized, exception.Message); + + // reset static instance. + GraphSession.Reset(); + } } } diff --git a/src/Authentication/Authentication/Common/GraphSession.cs b/src/Authentication/Authentication/Common/GraphSession.cs index e86d7157f5f..26a77ba2d42 100644 --- a/src/Authentication/Authentication/Common/GraphSession.cs +++ b/src/Authentication/Authentication/Common/GraphSession.cs @@ -93,10 +93,6 @@ public static void Initialize(Func instanceCreator, bool overwrite _instance = instanceCreator(); _initialized = true; } - else - { - throw new InvalidOperationException(string.Format(ErrorConstants.Message.InstanceExists, nameof(GraphSession), "Initialize(Func, bool)")); - } } finally { From e4eefa3557c32754c5237a53942a440300bec7e6 Mon Sep 17 00:00:00 2001 From: Peter Ombwa Date: Wed, 15 Jul 2020 16:42:34 -0700 Subject: [PATCH 2/2] Clean-up error constants. --- src/Authentication/Authentication/ErrorConstants.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Authentication/Authentication/ErrorConstants.cs b/src/Authentication/Authentication/ErrorConstants.cs index 88bf163e50b..50575b97efc 100644 --- a/src/Authentication/Authentication/ErrorConstants.cs +++ b/src/Authentication/Authentication/ErrorConstants.cs @@ -19,7 +19,6 @@ internal static class Message { internal const string InvalidJWT = "Invalid JWT access token."; internal const string MissingAuthContext = "Authentication needed, call Connect-Graph."; - internal const string InstanceExists = "An instance of {0} already exists. Call {1} to overwrite it."; internal const string NullOrEmptyParameter = "Parameter '{0}' cannot be null or empty."; internal const string MacKeyChainFailed = "{0} failed with result code {1}."; }