From 881ec3ed509ec187595cd9adf6c12ab0c53c6d1c Mon Sep 17 00:00:00 2001 From: sriv-e6x Date: Fri, 6 Sep 2024 16:34:50 +0530 Subject: [PATCH] add option to use async friendly datastore this comes as a property because I could not find a way to detect this automagically Signed-off-by: sriv-e6x --- Gauge.CSharp.Lib/DataStoreExtensions.cs | 34 ++++++++++++++++++++++++ Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj | 6 ++--- Gauge.CSharp.Lib/ScenarioDataStore.cs | 25 +++++++++++++++-- Gauge.CSharp.Lib/SpecDataStore.cs | 25 +++++++++++++++-- Gauge.CSharp.Lib/SuiteDataStore.cs | 27 ++++++++++++++++--- 5 files changed, 107 insertions(+), 10 deletions(-) diff --git a/Gauge.CSharp.Lib/DataStoreExtensions.cs b/Gauge.CSharp.Lib/DataStoreExtensions.cs index 0b678a4..c6b5ee1 100644 --- a/Gauge.CSharp.Lib/DataStoreExtensions.cs +++ b/Gauge.CSharp.Lib/DataStoreExtensions.cs @@ -39,5 +39,39 @@ internal static void Clear(this ThreadLocal store.Value.Clear(); } } + internal static object Get(this ConcurrentDictionary store, string key) + { + lock (store) + { + object outVal; + var valueExists = store.TryGetValue(key, out outVal); + return valueExists ? outVal : null; + } + } + + internal static T Get(this ConcurrentDictionary store, string key) + { + lock (store) + { + return (T)store.Get(key); + } + } + + internal static void Add(this ConcurrentDictionary store, string key, object value) + { + lock (store) + { + store[key] = value; + + } + } + + internal static void Clear(this ConcurrentDictionary store) + { + lock (store) + { + store.Clear(); + } + } } } \ No newline at end of file diff --git a/Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj b/Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj index 74e07be..db5e96c 100644 --- a/Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj +++ b/Gauge.CSharp.Lib/Gauge.CSharp.Lib.csproj @@ -3,9 +3,9 @@ net6.0;net7.0;net8.0 CSharp bindings for Gauge. Write CSharp step implementation for Gauge specs. https://gauge.org - 0.10.3 - 0.10.3.0 - 0.10.3.0 + 0.11.0 + 0.11.0.0 + 0.11.0.0 getgauge ThoughtWorks Inc. Copyright © ThoughtWorks Inc. 2018 diff --git a/Gauge.CSharp.Lib/ScenarioDataStore.cs b/Gauge.CSharp.Lib/ScenarioDataStore.cs index 0a9c93f..952fa89 100644 --- a/Gauge.CSharp.Lib/ScenarioDataStore.cs +++ b/Gauge.CSharp.Lib/ScenarioDataStore.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Concurrent; using System.Threading; @@ -7,24 +8,44 @@ public class ScenarioDataStore { private static ThreadLocal> store = new ThreadLocal>(() => new ConcurrentDictionary()); + private static ConcurrentDictionary asyncStore = new ConcurrentDictionary(); + public static object Get(string key) { - return store.Get(key); + return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static T Get(string key) { - return store.Get(key); + return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static void Add(string key, object value) { + if(UseAsyncFriendlyDatastore()) + { + asyncStore.Add(key, value); + return; + } store.Add(key, value); } public static void Clear() { + if(UseAsyncFriendlyDatastore()) + { + asyncStore.Clear(); + return; + } store.Clear(); } + + private static Boolean UseAsyncFriendlyDatastore() + { + var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores"); + if(String.IsNullOrEmpty(useAsyncFriendlyDatastore)) + return false; + return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase); + } } } \ No newline at end of file diff --git a/Gauge.CSharp.Lib/SpecDataStore.cs b/Gauge.CSharp.Lib/SpecDataStore.cs index 51a311a..e53bff9 100644 --- a/Gauge.CSharp.Lib/SpecDataStore.cs +++ b/Gauge.CSharp.Lib/SpecDataStore.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Concurrent; using System.Threading; @@ -7,24 +8,44 @@ public class SpecDataStore { private static ThreadLocal> store = new ThreadLocal>(() => new ConcurrentDictionary()); + private static ConcurrentDictionary asyncStore = new ConcurrentDictionary(); + public static object Get(string key) { - return store.Get(key); + return useAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static T Get(string key) { - return store.Get(key); + return useAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static void Add(string key, object value) { + if(useAsyncFriendlyDatastore()) + { + asyncStore.Add(key, value); + return; + } store.Add(key, value); } public static void Clear() { + if(useAsyncFriendlyDatastore()) + { + asyncStore.Clear(); + return; + } store.Clear(); } + + private static Boolean useAsyncFriendlyDatastore() + { + string useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores"); + if(String.IsNullOrEmpty(useAsyncFriendlyDatastore)) + return false; + return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase); + } } } \ No newline at end of file diff --git a/Gauge.CSharp.Lib/SuiteDataStore.cs b/Gauge.CSharp.Lib/SuiteDataStore.cs index b8a9d9c..f8ee744 100644 --- a/Gauge.CSharp.Lib/SuiteDataStore.cs +++ b/Gauge.CSharp.Lib/SuiteDataStore.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Concurrent; using System.Threading; @@ -7,24 +8,44 @@ public class SuiteDataStore { private static ThreadLocal> store = new ThreadLocal>(() => new ConcurrentDictionary()); + private static ConcurrentDictionary asyncStore = new ConcurrentDictionary(); + public static object Get(string key) { - return store.Get(key); + return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static T Get(string key) { - return store.Get(key); + return UseAsyncFriendlyDatastore() ? asyncStore.Get(key) : store.Get(key); } public static void Add(string key, object value) { + if(UseAsyncFriendlyDatastore()) + { + asyncStore.Add(key, value); + return; + } store.Add(key, value); } public static void Clear() { + if(UseAsyncFriendlyDatastore()) + { + asyncStore.Clear(); + return; + } store.Clear(); } + + private static Boolean UseAsyncFriendlyDatastore() + { + var useAsyncFriendlyDatastore = Environment.GetEnvironmentVariable("use_async_friendly_datastores"); + if(String.IsNullOrEmpty(useAsyncFriendlyDatastore)) + return false; + return useAsyncFriendlyDatastore.Equals("true", StringComparison.OrdinalIgnoreCase); + } } -} \ No newline at end of file +}