aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.UITests
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2019-02-27 16:49:42 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2019-02-27 16:49:42 +0200
commit2ab54573d10ca59351100ab8416178be2223fc91 (patch)
tree9a5077998e301c1e86cf00b07ea98eca501fc4a8 /Software/Visual_Studio/Utilities/Tango.UITests
parent88a73106e8113a4a6ce224f9205e11219939798e (diff)
downloadTango-2ab54573d10ca59351100ab8416178be2223fc91.tar.gz
Tango-2ab54573d10ca59351100ab8416178be2223fc91.zip
Added support for caching on machine studio.
Diffstat (limited to 'Software/Visual_Studio/Utilities/Tango.UITests')
-rw-r--r--Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml5
-rw-r--r--Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml.cs144
-rw-r--r--Software/Visual_Studio/Utilities/Tango.UITests/MyMemoryCache.cs215
-rw-r--r--Software/Visual_Studio/Utilities/Tango.UITests/Tango.UITests.csproj3
4 files changed, 279 insertions, 88 deletions
diff --git a/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml b/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml
index 6c1f5ed51..ab76a9745 100644
--- a/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml
+++ b/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml
@@ -20,8 +20,9 @@
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Center" Margin="0 0 0 10" x:Name="txtStatus"></TextBlock>
<ProgressBar x:Name="prog" VerticalAlignment="Center" Height="15" Width="700"></ProgressBar>
- <Button x:Name="btnStart" Click="btnStart_Click" HorizontalAlignment="Center" Padding="30 10" Margin="0 10 0 0">START</Button>
- <Button x:Name="btnChange" Click="btnChange_Click" HorizontalAlignment="Center" Padding="30 10" Margin="0 10 0 0">CHANGE</Button>
+ <Button x:Name="btnStart" Click="btnStart_Click" HorizontalAlignment="Center" Padding="30 10" Margin="0 10 0 0">LOAD JOBS</Button>
+ <Button x:Name="btnChange" Click="btnChange_Click" HorizontalAlignment="Center" Padding="30 10" Margin="0 10 0 0">CHANGE JOB</Button>
+ <Button x:Name="btnListen" Click="btnListen_Click" HorizontalAlignment="Center" Padding="30 10" Margin="0 10 0 0">START LISTEN</Button>
</StackPanel>
</Grid>
</Window>
diff --git a/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml.cs b/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml.cs
index 4ac7bffa2..ca676e277 100644
--- a/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml.cs
+++ b/Software/Visual_Studio/Utilities/Tango.UITests/MainWindow.xaml.cs
@@ -36,6 +36,7 @@ using System.Security.Permissions;
using TableDependency.SqlClient.Base;
using TableDependency.SqlClient;
using TableDependency.SqlClient.Base.EventArgs;
+using TableDependency.SqlClient.Base.Abstracts;
namespace Tango.UITests
{
@@ -44,93 +45,85 @@ namespace Tango.UITests
/// </summary>
public partial class MainWindow : Window
{
+ private ITableDependency _dep;
+
+ private DataSource _dataSource;
+
public MainWindow()
{
+ _dataSource = new DataSource()
+ {
+ Address = "twine.database.windows.net",
+ Catalog = "Tango_DEV",
+ Type = DataSourceType.SQLServer,
+ UserName = "Roy",
+ Password = "Aa123456",
+ IntegratedSecurity = false,
+ };
+
InitializeComponent();
- EntityFrameworkCache.Initialize(new InMemoryCache());
+ EntityFrameworkCache.Initialize(new MyMemoryCache() { Expiration = TimeSpan.FromMinutes(1) });
+ this.Closing += MainWindow_Closing;
}
- private static string _con = "data source=localhost\\SQLEXPRESS; initial catalog=Tango; integrated security=False;User Id=SqlDependency;Password=Aa123456";
+ private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
+ {
+ if (_dep != null)
+ {
+ _dep.Stop();
+ _dep.Dispose();
+ }
+ }
private void btnStart_Click(object sender, RoutedEventArgs e)
{
- //var dataSource = new DataSource()
- //{
- // Address = "localhost\\SQLEXPRESS",
- // Catalog = "Tango",
- // Type = DataSourceType.SQLServer,
- // IntegratedSecurity = false,
- // UserName = "Developer",
- // Password = "Aa123456",
- //};
+ Stopwatch watch = Stopwatch.StartNew();
- //var connection = dataSource.ToConnection();
+ using (ObservablesContext db = ObservablesContext.CreateDefault(_dataSource))
+ {
+ var users = db.Users.ToList();
+ var jobs = new JobsCollectionBuilder(db).SetAll().WithSegments().WithBrushStops().Build();
+ var first_job = jobs.First();
+
+ if (first_job.Name == "Changed Name 2")
+ {
+ Debug.WriteLine("Changed and cached !!!");
+ }
+ else
+ {
+ Debug.WriteLine("Not working.");
+ }
+ }
- String str = _con;
+ Debug.WriteLine($"Time to complete: {watch.Elapsed.TotalSeconds} seconds");
+ }
+
+ private void btnChange_Click(object sender, RoutedEventArgs e)
+ {
+ using (ObservablesContext db = ObservablesContext.CreateDefault(_dataSource))
+ {
+ var jobs = new JobsCollectionBuilder(db).SetAll().WithSegments().WithBrushStops().Build();
+ var first_job = jobs.First();
+ first_job.Name = "Changed Name 2";
+ db.SaveChanges();
+ }
+ }
- //var mapper = new ModelToTableMapper<User>();
- //mapper.AddMapping(c => c.ID, "ID");
- //mapper.AddMapping(c => c.Email, "EMAIL");
+ private void btnListen_Click(object sender, RoutedEventArgs e)
+ {
+ String str = _dataSource.ToConnection().ConnectionString;
var dep = new SqlTableDependency<DAL.Remote.DB.USER>(str, "USERS", "dbo");
dep.TraceLevel = TraceLevel.Verbose;
- dep.TraceListener = new TextWriterTraceListener(Console.Out);
+ dep.TraceListener = new TextWriterTraceListener(Console.Out);
dep.OnChanged += Changed;
dep.Start();
- //SqlClientPermission clientPermission =
- // new SqlClientPermission(PermissionState.Unrestricted);
- //clientPermission.Demand();
-
- //System.Data.SqlClient.SqlDependency.Start(str);
-
- //String commandText = "SELECT [dbo].[USERS].[EMAIL] FROM [dbo].[USERS];";
- //SqlCommand command = new SqlCommand(commandText, connection as SqlConnection);
- //command.Notification = null;
- //command.CommandType = CommandType.Text;
-
- //command.Connection.Open();
-
- //SqlDependency dependency = new SqlDependency(command);
- //dependency.OnChange += (s, args) =>
- //{
-
- //};
-
- //command.ExecuteReader();
- //String a = "!23";
-
- //Stopwatch watch = Stopwatch.StartNew();
-
- //using (ObservablesContext db = ObservablesContext.CreateDefault(new DataSource()
- //{
- // Address = "localhost\\SQLEXPRESS",
- // Catalog = "Tango",
- // Type = DataSourceType.SQLServer,
- // UserName = "Roy",
- // Password = "Aa123456",
- // IntegratedSecurity = true,
- //}))
- //{
- // var users = db.Users.ToList();
- // var jobs = new JobsCollectionBuilder(db).SetAll().WithSegments().WithBrushStops().Build();
- // var first_job = jobs.First();
-
- // if (first_job.Name == "Changed Name 2")
- // {
- // Debug.WriteLine("Changed and cached !!!");
- // }
- // else
- // {
- // Debug.WriteLine("Not working.");
- // }
- //}
-
- //Debug.WriteLine($"Time to complete: {watch.Elapsed.TotalSeconds} seconds");
+ _dep = dep;
}
private void Changed(object sender, RecordChangedEventArgs<DAL.Remote.DB.USER> e)
@@ -141,24 +134,5 @@ namespace Tango.UITests
Console.WriteLine(changedEntity.ToJsonString());
}
-
- private void btnChange_Click(object sender, RoutedEventArgs e)
- {
- using (ObservablesContext db = ObservablesContext.CreateDefault(new DataSource()
- {
- Address = "twine.database.windows.net",
- Catalog = "Tango_DEV",
- Type = DataSourceType.SQLServer,
- UserName = "Roy",
- Password = "Aa123456",
- IntegratedSecurity = false,
- }))
- {
- var jobs = new JobsCollectionBuilder(db).SetAll().WithSegments().WithBrushStops().Build();
- var first_job = jobs.First();
- first_job.Name = "Changed Name 2";
- db.SaveChanges();
- }
- }
}
}
diff --git a/Software/Visual_Studio/Utilities/Tango.UITests/MyMemoryCache.cs b/Software/Visual_Studio/Utilities/Tango.UITests/MyMemoryCache.cs
new file mode 100644
index 000000000..0a3583f54
--- /dev/null
+++ b/Software/Visual_Studio/Utilities/Tango.UITests/MyMemoryCache.cs
@@ -0,0 +1,215 @@
+
+namespace EFCache
+{
+ using System;
+ using System.Linq;
+ using System.Collections.Generic;
+
+ public class MyMemoryCache : ICache
+ {
+ private readonly Dictionary<string, CacheEntry> _cache = new Dictionary<string, CacheEntry>();
+ private readonly Dictionary<string, HashSet<string>> _entitySetToKey = new Dictionary<string, HashSet<string>>();
+
+ public TimeSpan Expiration { get; set; }
+
+ public bool GetItem(string key, out object value)
+ {
+ if (key == null)
+ {
+ throw new ArgumentNullException("key");
+ }
+
+ value = null;
+
+ lock (_cache)
+ {
+ var now = DateTimeOffset.Now;
+
+ CacheEntry entry;
+ if (_cache.TryGetValue(key, out entry))
+ {
+ if (EntryExpired(entry, now))
+ {
+ InvalidateItem(key);
+ }
+ else
+ {
+ //entry.LastAccess = now;
+ value = entry.Value;
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ public void PutItem(string key, object value, IEnumerable<string> dependentEntitySets, TimeSpan slidingExpiration, DateTimeOffset absoluteExpiration)
+ {
+ if (key == null)
+ {
+ throw new ArgumentNullException("key");
+ }
+
+ if (dependentEntitySets == null)
+ {
+ throw new ArgumentNullException("dependentEntitySets");
+ }
+
+ lock (_cache)
+ {
+ var entitySets = dependentEntitySets.ToArray();
+
+ _cache[key] = new CacheEntry(value, entitySets, slidingExpiration, absoluteExpiration);
+
+ foreach (var entitySet in entitySets)
+ {
+ HashSet<string> keys;
+
+ if (!_entitySetToKey.TryGetValue(entitySet, out keys))
+ {
+ keys = new HashSet<string>();
+ _entitySetToKey[entitySet] = keys;
+ }
+
+ keys.Add(key);
+ }
+ }
+ }
+
+ public void InvalidateSets(IEnumerable<string> entitySets)
+ {
+ if (entitySets == null)
+ {
+ throw new ArgumentNullException("entitySets");
+ }
+
+ lock (_cache)
+ {
+ var itemsToInvalidate = new HashSet<string>();
+
+ foreach (var entitySet in entitySets)
+ {
+ HashSet<string> keys;
+
+ if (_entitySetToKey.TryGetValue(entitySet, out keys))
+ {
+ itemsToInvalidate.UnionWith(keys);
+
+ _entitySetToKey.Remove(entitySet);
+ }
+ }
+
+ foreach (var key in itemsToInvalidate)
+ {
+ InvalidateItem(key);
+ }
+ }
+ }
+
+ public void InvalidateItem(string key)
+ {
+ if (key == null)
+ {
+ throw new ArgumentNullException("key");
+ }
+
+ lock (_cache)
+ {
+ CacheEntry entry;
+
+ if (_cache.TryGetValue(key, out entry))
+ {
+ _cache.Remove(key);
+
+ foreach (var set in entry.EntitySets)
+ {
+ HashSet<string> keys;
+ if (_entitySetToKey.TryGetValue(set, out keys))
+ {
+ keys.Remove(key);
+ }
+ }
+ }
+ }
+ }
+
+ public void Purge()
+ {
+ Purge(false);
+ }
+
+ public void Purge(bool removeUnexpiredItems)
+ {
+ lock (_cache)
+ {
+ var now = DateTimeOffset.Now;
+ var itemsToRemove = new HashSet<string>();
+
+ foreach (var item in _cache)
+ {
+ if (removeUnexpiredItems || EntryExpired(item.Value, now))
+ {
+ itemsToRemove.Add(item.Key);
+ }
+ }
+
+ foreach (var key in itemsToRemove)
+ {
+ InvalidateItem(key);
+ }
+ }
+ }
+
+ public int Count
+ {
+ get { return _cache.Count; }
+ }
+
+ private bool EntryExpired(CacheEntry entry, DateTimeOffset now)
+ {
+ return entry.AbsoluteExpiration < now || (now - entry.LastAccess) > Expiration;
+ }
+
+ private class CacheEntry
+ {
+ private readonly object _value;
+ private readonly string[] _entitySets;
+ private readonly TimeSpan _slidingExpiration;
+ private readonly DateTimeOffset _absoluteExpiration;
+ private readonly DateTime _created;
+
+ public CacheEntry(object value, string[] entitySets, TimeSpan slidingExpiration,
+ DateTimeOffset absoluteExpiration)
+ {
+ _value = value;
+ _entitySets = entitySets;
+ _slidingExpiration = slidingExpiration;
+ _absoluteExpiration = absoluteExpiration;
+ LastAccess = DateTimeOffset.Now;
+ }
+
+ public object Value
+ {
+ get { return _value; }
+ }
+
+ public string[] EntitySets
+ {
+ get { return _entitySets; }
+ }
+
+ public TimeSpan SlidingExpiration
+ {
+ get { return _slidingExpiration; }
+ }
+
+ public DateTimeOffset AbsoluteExpiration
+ {
+ get { return _absoluteExpiration; }
+ }
+
+ public DateTimeOffset LastAccess { get; set; }
+ }
+ }
+} \ No newline at end of file
diff --git a/Software/Visual_Studio/Utilities/Tango.UITests/Tango.UITests.csproj b/Software/Visual_Studio/Utilities/Tango.UITests/Tango.UITests.csproj
index 7daf19294..87643ac2b 100644
--- a/Software/Visual_Studio/Utilities/Tango.UITests/Tango.UITests.csproj
+++ b/Software/Visual_Studio/Utilities/Tango.UITests/Tango.UITests.csproj
@@ -99,6 +99,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
+ <Compile Include="MyMemoryCache.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
@@ -182,7 +183,7 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
- <UserProperties BuildVersion_StartDate="2000/1/1" BuildVersion_UseGlobalSettings="False" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" />
+ <UserProperties BuildVersion_AssemblyInfoFilename="Properties\AssemblyInfo.cs" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" BuildVersion_UseGlobalSettings="False" BuildVersion_StartDate="2000/1/1" />
</VisualStudio>
</ProjectExtensions>
</Project> \ No newline at end of file