mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
🚧 Tests for MemoryCache
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Kruzya.TelegramBot.Core.Cache
|
||||
{
|
||||
public interface ICacheStorage<TKey, TValue>
|
||||
{
|
||||
public bool TryGetValue(TKey key, ref TValue value);
|
||||
public bool TryGetValue(TKey key, out TValue value);
|
||||
public void SetValue(TKey key, TValue value, int timeToLive);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using SQLitePCL;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Cache
|
||||
{
|
||||
@@ -47,9 +48,10 @@ namespace Kruzya.TelegramBot.Core.Cache
|
||||
_defaultTtl = defaultTtl;
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, ref TValue value)
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (dict.ContainsKey(key))
|
||||
value = default;
|
||||
if (!dict.ContainsKey(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CombotAntiSpam", "modules\C
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RichSiteSummary", "modules\RichSiteSummary\RichSiteSummary.csproj", "{C50D64C3-204B-4B58-927B-C8D759787252}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "tests\Tests.csproj", "{5C62CA70-F270-4029-953E-458623C31A3B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -26,6 +28,10 @@ Global
|
||||
{C50D64C3-204B-4B58-927B-C8D759787252}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C50D64C3-204B-4B58-927B-C8D759787252}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C50D64C3-204B-4B58-927B-C8D759787252}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5C62CA70-F270-4029-953E-458623C31A3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5C62CA70-F270-4029-953E-458623C31A3B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Kruzya.TelegramBot.Core.Cache;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Kruzya.TelegramBot.Core.Tests
|
||||
{
|
||||
public class MemoryCache
|
||||
{
|
||||
[Test]
|
||||
public void GetSetItem()
|
||||
{
|
||||
var cache = new MemoryCache<string, int>();
|
||||
|
||||
int id;
|
||||
Assert.IsFalse(cache.TryGetValue("user", out id), "GetValue() returned something");
|
||||
|
||||
id = 5;
|
||||
cache.SetValue("user", id);
|
||||
|
||||
id = 10;
|
||||
Assert.IsTrue(cache.TryGetValue("user", out id), "GetValue() doesn't returned any value");
|
||||
Assert.IsTrue(id == 5, "GetValue() returned don't expected value");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TimeToLive()
|
||||
{
|
||||
var cache = new MemoryCache<string, int>();
|
||||
cache.SetValue("user", 5, 1);
|
||||
cache.SetValue("admin", 10, 2);
|
||||
cache.SetValue("manager", 15, Timeout.Infinite);
|
||||
|
||||
int id;
|
||||
Thread.Sleep(TimeSpan.FromSeconds(1));
|
||||
Assert.IsFalse(cache.TryGetValue("user", out id), "GetValue() returned expired entry (user).");
|
||||
Assert.IsTrue(cache.TryGetValue("admin", out id), "GetValue() doesn't returned live entry.");
|
||||
Assert.IsTrue(id == 10, "GetValue() returned unexpected entry.");
|
||||
|
||||
Thread.Sleep(TimeSpan.FromSeconds(1));
|
||||
Assert.IsFalse(cache.TryGetValue("admin", out id), "GetValue() returned expired entry (admin).");
|
||||
Assert.IsTrue(cache.TryGetValue("manager", out id), "GetValue() doesn't returned live entry.");
|
||||
Assert.IsTrue(id == 15, "GetValue() returned unexpected entry.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
|
||||
<AssemblyName>TelegramBot.Test</AssemblyName>
|
||||
|
||||
<RootNamespace>Kruzya.TelegramBot.Core.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="nunit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user