Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public bool TryGet(out string? fileName)

public int CountFiles()
{
return _files.Count();
return _files.Count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public bool TryServeUser(out User? user)

public int CountUsersInLine()
{
return _users.Count();
return _users.Count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public bool TryGetTop(out string? color)

public int Count()
{
return _paperColor.Count();
return _paperColor.Count;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using ExampleApp.Helpers;
using ExampleApp.Helpers;
using System.Collections.Concurrent;

namespace ExampleApp.DictionaryExamples;
Expand Down Expand Up @@ -27,7 +26,7 @@ public IDictionary<string, User> GetAll()

public void PrintAll()
{
Console.WriteLine($"No. of Records: {_record.Count()}");
Console.WriteLine($"No. of Records: {_record.Count}");
foreach (var (role, user) in _record)
{
Console.WriteLine($"Role: {role}, User: {user}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IDictionary<string, User> GetAll()

public void PrintAll()
{
Console.WriteLine($"No. of Records: {_record.Count()}");
Console.WriteLine($"No. of Records: {_record.Count}");
foreach (var (role, user) in _record)
{
Console.WriteLine($"Role: {role}, User: {user}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class UserProvider

public static async Task<User> GetUser(int id)
{
await Task.Delay((int)TimeSpan.FromSeconds(1).TotalMilliseconds);
await Task.Delay(TimeSpan.FromSeconds(1));

return _users[id];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Concurrent;

namespace ExampleApp.ListAndSetExamples;

public static class ListAndSetAlternatives
{
public static int CountUniqueIds(int workers, int idsPerWorker)
{
var seenIds = new ConcurrentDictionary<int, byte>();

Parallel.For(0, workers, _ =>
{
for (var id = 0; id < idsPerWorker; id++)
{
seenIds.TryAdd(id, 0);
}
});

return seenIds.Count;
}

public static int AppendToQueue(int workers, int itemsPerWorker)
{
var entries = new ConcurrentQueue<int>();

Parallel.For(0, workers, worker =>
{
for (var i = 0; i < itemsPerWorker; i++)
{
entries.Enqueue(worker * itemsPerWorker + i);
}
});

return entries.Count;
}

public static int AppendToLockedList(int workers, int itemsPerWorker)
{
var numbers = new List<int>();
var numbersLock = new Lock();

Parallel.For(0, workers, worker =>
{
for (var i = 0; i < itemsPerWorker; i++)
{
lock (numbersLock)
{
numbers.Add(worker * itemsPerWorker + i);
}
}
});

return numbers.Count;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using ExampleApp.DictionaryExamples;
using ExampleApp.ListAndSetExamples;

// Console.WriteLine("RunRoleManagerDictionaryExampleConcurrent()");
// await RunRoleManagerDictionaryExampleConcurrent();
if (args.Contains("--race"))
{
Console.WriteLine("RunRoleManagerDictionaryExampleConcurrent()");
await RunRoleManagerDictionaryExampleConcurrent();

return;
}

Console.WriteLine("RunRoleManagerDictionaryExampleSync()");
await RunRoleManagerDictionaryExampleSync();

// Console.WriteLine("\n\n");
Console.WriteLine("RunRoleManagerConcurrentDictionaryExampleConcurrent()");
await RunRoleManagerConcurrentDictionaryExampleConcurrent();

Console.WriteLine($"Unique ids: {ListAndSetAlternatives.CountUniqueIds(workers: 4, idsPerWorker: 1000)}");
Console.WriteLine($"Queued entries: {ListAndSetAlternatives.AppendToQueue(workers: 4, itemsPerWorker: 1000)}");
Console.WriteLine($"Locked list entries: {ListAndSetAlternatives.AppendToLockedList(workers: 4, itemsPerWorker: 1000)}");

static async Task RunRoleManagerDictionaryExampleSync()
{
var roleManager = new RoleManagerDictionary();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using ExampleApp.ListAndSetExamples;

namespace Tests;

public class ListAndSetAlternativesTests
{
[Fact]
public void GivenOverlappingIds_WhenAddedFromSeveralThreads_ThenEachIdIsStoredOnce()
{
var count = ListAndSetAlternatives.CountUniqueIds(workers: 4, idsPerWorker: 1000);

Assert.Equal(1000, count);
}

[Fact]
public void GivenSeveralWriters_WhenAppendingToConcurrentQueue_ThenNoItemIsLost()
{
var count = ListAndSetAlternatives.AppendToQueue(workers: 4, itemsPerWorker: 1000);

Assert.Equal(4000, count);
}

[Fact]
public void GivenSeveralWriters_WhenAppendingToLockedList_ThenNoItemIsLost()
{
var count = ListAndSetAlternatives.AppendToLockedList(workers: 4, itemsPerWorker: 1000);

Assert.Equal(4000, count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task GivenColors_WhenAddIsInvokedConcurrently_ThenColorsAreAdded()
}

[Fact]
public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue()
public void GivenColor_WhenTryUseIsInvoked_ThenColorIsRemovedAndReturnsTrue()
{
var color = "white";
var papers = new PrinterColorPaperConcurrentStack();
Expand All @@ -31,7 +31,7 @@ public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue
}

[Fact]
public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrivedAndReturnsTrue()
public void GivenColor_WhenTryGetTopIsInvoked_ThenColorIsRetrievedAndReturnsTrue()
{
var color = "white";
var papers = new PrinterColorPaperConcurrentStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public async Task GivenDifferentRoles_WhenTryAssignIsInvokedConcurrently_ThenAll
await Task.WhenAll(task1, task2, task3);
var roles = concurrentRoleManager.GetAll();
var firstRoleExits = roles.TryGetValue(role, out User? user);
var secondRoleExits = roles.TryGetValue(role, out User? user1);
var thirdRoleExits = roles.TryGetValue(role, out User? user2);
var secondRoleExits = roles.TryGetValue(role1, out User? user1);
var thirdRoleExits = roles.TryGetValue(role2, out User? user2);

Assert.True(firstRoleExits);
Assert.True(secondRoleExits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class RoleManagerDictionaryTests
public async Task GivenIdenticalRoles_WhenTryAssignIsInvoked_ThenOnlyOneUserIsAssigned()
{
var role = "Admin";
var concurrentRoleManager = new RoleManagerConcurrentDictionary();
await concurrentRoleManager.TryAssign(role, 1);
await concurrentRoleManager.TryAssign(role, 2);
await concurrentRoleManager.TryAssign(role, 3);
var roleManager = new RoleManagerDictionary();
await roleManager.TryAssign(role, 1);
await roleManager.TryAssign(role, 2);
await roleManager.TryAssign(role, 3);

var roles = concurrentRoleManager.GetAll();
var roles = roleManager.GetAll();
var valueExists = roles.TryGetValue(role, out User? user);

Assert.True(valueExists);
Expand All @@ -28,15 +28,15 @@ public async Task GivenDifferentRoles_WhenTryAssignIsInvoked_ThenAllUserAreAssig
var role = "Admin";
var role1 = "Dev";
var role2 = "User";
var concurrentRoleManager = new RoleManagerConcurrentDictionary();
await concurrentRoleManager.TryAssign(role, 1);
await concurrentRoleManager.TryAssign(role1, 2);
await concurrentRoleManager.TryAssign(role2, 3);
var roleManager = new RoleManagerDictionary();
await roleManager.TryAssign(role, 1);
await roleManager.TryAssign(role1, 2);
await roleManager.TryAssign(role2, 3);

var roles = concurrentRoleManager.GetAll();
var roles = roleManager.GetAll();
var firstRoleExits = roles.TryGetValue(role, out User? user);
var secondRoleExits = roles.TryGetValue(role, out User? user1);
var thirdRoleExits = roles.TryGetValue(role, out User? user2);
var secondRoleExits = roles.TryGetValue(role1, out User? user1);
var thirdRoleExits = roles.TryGetValue(role2, out User? user2);

Assert.True(firstRoleExits);
Assert.True(secondRoleExits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public async Task GivenFileNames_WhenAddFileIsInvokedConcurrently_ThenFilesAreAd
{
var storage = new StorageConcurrentBag();
var task1 = Task.Run(() => storage.AddNew("sample.txt"));
var task2 = Task.Run(() => storage.AddNew("awsome.png"));
var task2 = Task.Run(() => storage.AddNew("awesome.png"));
var task3 = Task.Run(() => storage.AddNew("sample.pdf"));

await Task.WhenAll(task1, task2, task3);
Expand All @@ -20,7 +20,7 @@ public async Task GivenFileNames_WhenAddFileIsInvokedConcurrently_ThenFilesAreAd
[Fact]
public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue()
{
var fileName = "awsome.png";
var fileName = "awesome.png";
var storage = new StorageConcurrentBag();
storage.AddNew(fileName);
var result = storage.TryRemove(out string? removedFile);
Expand All @@ -30,9 +30,9 @@ public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue
}

[Fact]
public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrivedAndReturnsTrue()
public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrievedAndReturnsTrue()
{
var fileName = "awsome.png";
var fileName = "awesome.png";
var storage = new StorageConcurrentBag();
storage.AddNew(fileName);
var result = storage.TryGet(out string? retrievedFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading