Skip to content

Commit db65880

Browse files
temp
1 parent 0880def commit db65880

File tree

3 files changed

+444
-358
lines changed

3 files changed

+444
-358
lines changed
Lines changed: 184 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,184 @@
1-
using FluentAssertions;
2-
using Microsoft.Coyote;
3-
using Microsoft.Coyote.Actors;
4-
using Microsoft.Coyote.Runtime;
5-
using Microsoft.Coyote.SystematicTesting;
6-
using WouterVanRanst.Utils.Collections;
7-
using Xunit.Abstractions;
8-
9-
namespace WouterVanRanst.Utils.Tests;
10-
11-
public class ConcurrentConsumingTaskCollectionCoyoteTests
12-
{
13-
ITestOutputHelper Output;
14-
15-
public ConcurrentConsumingTaskCollectionCoyoteTests(ITestOutputHelper output)
16-
{
17-
this.Output = output;
18-
}
19-
20-
21-
[Fact]
22-
public void TestTaskQueueSingleProducerSingleConsumer()
23-
{
24-
return;
25-
26-
var configuration = Configuration.Create()
27-
.WithReproducibleTrace(File.ReadAllText("C:\\Users\\WouterVanRanst\\Desktop\\mytest.trace"))
28-
//.WithDeadlockTimeout(10000)
29-
//.WithVerbosityEnabled()
30-
;
31-
var engine = TestingEngine.Create(configuration, this.TestSingleProducerSingleConsumer);
32-
engine.Run();
33-
var report = engine.TestReport;
34-
Output.WriteLine("Coyote found {0} bug.", report.NumOfFoundBugs);
35-
36-
engine.TryEmitReports("C:\\Users\\WouterVanRanst\\Desktop", "mytest", out var filenames);
37-
//foreach (var item in filenames)
38-
//{
39-
// Output.WriteLine("See log file: {0}", item);
40-
//}
41-
42-
Assert.Equal(0, engine.TestReport.NumOfFoundBugs);
43-
}
44-
45-
[Fact]
46-
public void TestTaskQueueMultipleProducersMultipleConsumers()
47-
{
48-
return;
49-
50-
var configuration = Configuration.Create()
51-
//.WithReproducibleTrace(File.ReadAllText("C:\\Users\\WouterVanRanst\\Desktop\\mytest2.trace"))
52-
//.WithTestingIterations(100)
53-
;
54-
var engine = TestingEngine.Create(configuration, this.TestMultipleProducersMultipleConsumers);
55-
engine.Run();
56-
57-
//engine.TryEmitReports("C:\\Users\\WouterVanRanst\\Desktop", "mytest2", out var filenames);
58-
59-
Assert.Equal(0, engine.TestReport.NumOfFoundBugs);
60-
}
61-
62-
private async Task TestSingleProducerSingleConsumer(IActorRuntime runtime)
63-
{
64-
var taskQueue = new ConcurrentConsumingTaskCollection<string>();
65-
var actualOrder = new List<string>();
66-
var expectedOrder = new List<string> { "Task2", "Task3", "Task1" }; // Expected order based on task delays
67-
68-
// Producer: Add tasks to the queue
69-
var p1 = Task.Run(async () =>
70-
{
71-
taskQueue.Add(SimulateTask("Task1", 300)); // Long-running task
72-
taskQueue.Add(SimulateTask("Task2", 100)); // Short-running task
73-
taskQueue.Add(SimulateTask("Task3", 200)); // Medium-running task
74-
taskQueue.CompleteAdding();
75-
});
76-
77-
// Consumer: Consume tasks in completion order and store the result
78-
var c1 = Task.Run(async () =>
79-
{
80-
await foreach (var result in taskQueue.GetConsumingEnumerable())
81-
{
82-
//SchedulingPoint.Suppress();
83-
actualOrder.Add(result.Result);
84-
Output.WriteLine(result.Result);
85-
//SchedulingPoint.Resume();
86-
}
87-
});
88-
89-
await Task.WhenAll(p1, c1);
90-
//Output.WriteLine(c1.Status.ToString());
91-
92-
93-
// Assert that the tasks were processed in the correct order
94-
//expectedOrder.SequenceEqual(actualOrder).Should().BeTrue();
95-
Assert.Equal(expectedOrder, actualOrder);
96-
}
97-
98-
private async Task TestMultipleProducersMultipleConsumers(IActorRuntime runtime)
99-
{
100-
//var expectedOrder = new List<string> { "Producer1", "Producer2", "Producer3" };
101-
//var actualOrder = new string[expectedOrder.Count];
102-
//var currentIndex = -1;
103-
104-
//var t1 = Task.Run(async () =>
105-
//{
106-
// await Task.Delay(1000);
107-
// var index = Interlocked.Increment(ref currentIndex);
108-
// actualOrder[index] = "Producer1";
109-
//});
110-
111-
//var t2 = Task.Run(async () =>
112-
//{
113-
// await Task.Delay(2000);
114-
// var index = Interlocked.Increment(ref currentIndex);
115-
// actualOrder[index] = "Producer2";
116-
//});
117-
118-
//var t3 = Task.Run(async () =>
119-
//{
120-
// await Task.Delay(3000);
121-
// var index = Interlocked.Increment(ref currentIndex);
122-
// actualOrder[index] = "Producer3";
123-
//});
124-
125-
//await Task.WhenAll(t1, t2, t3);
126-
//Assert.Equal(expectedOrder, actualOrder);
127-
128-
var taskQueue = new ConcurrentConsumingTaskCollection<string>();
129-
var expectedOrder = new List<string> { "Producer1_Task2", "Producer2_Task2", "Producer2_Task1", "Producer1_Task1" };
130-
var actualOrder = new string[expectedOrder.Count];
131-
132-
var currentIndex = -1;
133-
134-
// Producer 1: Add tasks to the queue
135-
var p1 = Task.Run(() =>
136-
{
137-
taskQueue.Add(SimulateTask("Producer1_Task1", 3000)); // Long-running task
138-
taskQueue.Add(SimulateTask("Producer1_Task2", 1000)); // Short-running task
139-
});
140-
141-
// Producer 2: Add tasks to the queue
142-
var p2 = Task.Run(() =>
143-
{
144-
taskQueue.Add(SimulateTask("Producer2_Task1", 2000)); // Medium-running task
145-
taskQueue.Add(SimulateTask("Producer2_Task2", 1500)); // Medium-short task
146-
});
147-
148-
Task.WhenAll(p1, p2).ContinueWith(_ => taskQueue.CompleteAdding());
149-
150-
// Consumer 1: Consume tasks in completion order and store the result
151-
var c1 = Task.Run(async () =>
152-
{
153-
await foreach (var result in taskQueue.GetConsumingEnumerable())
154-
{
155-
var index = Interlocked.Increment(ref currentIndex);
156-
actualOrder[index] = result.Result;
157-
await Task.Yield();
158-
}
159-
});
160-
161-
// Consumer 2: Consume tasks in completion order and store the result
162-
var c2 = Task.Run(async () =>
163-
{
164-
await foreach (var result in taskQueue.GetConsumingEnumerable())
165-
{
166-
var index = Interlocked.Increment(ref currentIndex);
167-
actualOrder[index] = result.Result;
168-
await Task.Yield();
169-
}
170-
});
171-
172-
await Task.WhenAll(c1, c2);
173-
174-
// Assert that the tasks were processed in the correct order
175-
Assert.Equal(expectedOrder, actualOrder);
176-
}
177-
178-
private async Task<string> SimulateTask(string name, int delay)
179-
{
180-
//Thread.Sleep(delay);
181-
await Task.Delay(delay); // Simulate work
182-
return name;
183-
}
184-
}
1+
//using FluentAssertions;
2+
//using Microsoft.Coyote;
3+
//using Microsoft.Coyote.Actors;
4+
//using Microsoft.Coyote.Runtime;
5+
//using Microsoft.Coyote.SystematicTesting;
6+
//using WouterVanRanst.Utils.Collections;
7+
//using Xunit.Abstractions;
8+
9+
//namespace WouterVanRanst.Utils.Tests;
10+
11+
//public class ConcurrentConsumingTaskCollectionCoyoteTests
12+
//{
13+
// ITestOutputHelper Output;
14+
15+
// public ConcurrentConsumingTaskCollectionCoyoteTests(ITestOutputHelper output)
16+
// {
17+
// this.Output = output;
18+
// }
19+
20+
21+
// [Fact]
22+
// public void TestTaskQueueSingleProducerSingleConsumer()
23+
// {
24+
// return;
25+
26+
// var configuration = Configuration.Create()
27+
// .WithReproducibleTrace(File.ReadAllText("C:\\Users\\WouterVanRanst\\Desktop\\mytest.trace"))
28+
// //.WithDeadlockTimeout(10000)
29+
// //.WithVerbosityEnabled()
30+
// ;
31+
// var engine = TestingEngine.Create(configuration, this.TestSingleProducerSingleConsumer);
32+
// engine.Run();
33+
// var report = engine.TestReport;
34+
// Output.WriteLine("Coyote found {0} bug.", report.NumOfFoundBugs);
35+
36+
// engine.TryEmitReports("C:\\Users\\WouterVanRanst\\Desktop", "mytest", out var filenames);
37+
// //foreach (var item in filenames)
38+
// //{
39+
// // Output.WriteLine("See log file: {0}", item);
40+
// //}
41+
42+
// Assert.Equal(0, engine.TestReport.NumOfFoundBugs);
43+
// }
44+
45+
// [Fact]
46+
// public void TestTaskQueueMultipleProducersMultipleConsumers()
47+
// {
48+
// return;
49+
50+
// var configuration = Configuration.Create()
51+
// //.WithReproducibleTrace(File.ReadAllText("C:\\Users\\WouterVanRanst\\Desktop\\mytest2.trace"))
52+
// //.WithTestingIterations(100)
53+
// ;
54+
// var engine = TestingEngine.Create(configuration, this.TestMultipleProducersMultipleConsumers);
55+
// engine.Run();
56+
57+
// //engine.TryEmitReports("C:\\Users\\WouterVanRanst\\Desktop", "mytest2", out var filenames);
58+
59+
// Assert.Equal(0, engine.TestReport.NumOfFoundBugs);
60+
// }
61+
62+
// private async Task TestSingleProducerSingleConsumer(IActorRuntime runtime)
63+
// {
64+
// var taskQueue = new ConcurrentConsumingTaskCollection<string>();
65+
// var actualOrder = new List<string>();
66+
// var expectedOrder = new List<string> { "Task2", "Task3", "Task1" }; // Expected order based on task delays
67+
68+
// // Producer: Add tasks to the queue
69+
// var p1 = Task.Run(async () =>
70+
// {
71+
// taskQueue.Add(SimulateTask("Task1", 300)); // Long-running task
72+
// taskQueue.Add(SimulateTask("Task2", 100)); // Short-running task
73+
// taskQueue.Add(SimulateTask("Task3", 200)); // Medium-running task
74+
// taskQueue.CompleteAdding();
75+
// });
76+
77+
// // Consumer: Consume tasks in completion order and store the result
78+
// var c1 = Task.Run(async () =>
79+
// {
80+
// await foreach (var result in taskQueue.GetConsumingEnumerable())
81+
// {
82+
// //SchedulingPoint.Suppress();
83+
// actualOrder.Add(result.Result);
84+
// Output.WriteLine(result.Result);
85+
// //SchedulingPoint.Resume();
86+
// }
87+
// });
88+
89+
// await Task.WhenAll(p1, c1);
90+
// //Output.WriteLine(c1.Status.ToString());
91+
92+
93+
// // Assert that the tasks were processed in the correct order
94+
// //expectedOrder.SequenceEqual(actualOrder).Should().BeTrue();
95+
// Assert.Equal(expectedOrder, actualOrder);
96+
// }
97+
98+
// private async Task TestMultipleProducersMultipleConsumers(IActorRuntime runtime)
99+
// {
100+
// //var expectedOrder = new List<string> { "Producer1", "Producer2", "Producer3" };
101+
// //var actualOrder = new string[expectedOrder.Count];
102+
// //var currentIndex = -1;
103+
104+
// //var t1 = Task.Run(async () =>
105+
// //{
106+
// // await Task.Delay(1000);
107+
// // var index = Interlocked.Increment(ref currentIndex);
108+
// // actualOrder[index] = "Producer1";
109+
// //});
110+
111+
// //var t2 = Task.Run(async () =>
112+
// //{
113+
// // await Task.Delay(2000);
114+
// // var index = Interlocked.Increment(ref currentIndex);
115+
// // actualOrder[index] = "Producer2";
116+
// //});
117+
118+
// //var t3 = Task.Run(async () =>
119+
// //{
120+
// // await Task.Delay(3000);
121+
// // var index = Interlocked.Increment(ref currentIndex);
122+
// // actualOrder[index] = "Producer3";
123+
// //});
124+
125+
// //await Task.WhenAll(t1, t2, t3);
126+
// //Assert.Equal(expectedOrder, actualOrder);
127+
128+
// var taskQueue = new ConcurrentConsumingTaskCollection<string>();
129+
// var expectedOrder = new List<string> { "Producer1_Task2", "Producer2_Task2", "Producer2_Task1", "Producer1_Task1" };
130+
// var actualOrder = new string[expectedOrder.Count];
131+
132+
// var currentIndex = -1;
133+
134+
// // Producer 1: Add tasks to the queue
135+
// var p1 = Task.Run(() =>
136+
// {
137+
// taskQueue.Add(SimulateTask("Producer1_Task1", 3000)); // Long-running task
138+
// taskQueue.Add(SimulateTask("Producer1_Task2", 1000)); // Short-running task
139+
// });
140+
141+
// // Producer 2: Add tasks to the queue
142+
// var p2 = Task.Run(() =>
143+
// {
144+
// taskQueue.Add(SimulateTask("Producer2_Task1", 2000)); // Medium-running task
145+
// taskQueue.Add(SimulateTask("Producer2_Task2", 1500)); // Medium-short task
146+
// });
147+
148+
// Task.WhenAll(p1, p2).ContinueWith(_ => taskQueue.CompleteAdding());
149+
150+
// // Consumer 1: Consume tasks in completion order and store the result
151+
// var c1 = Task.Run(async () =>
152+
// {
153+
// await foreach (var result in taskQueue.GetConsumingEnumerable())
154+
// {
155+
// var index = Interlocked.Increment(ref currentIndex);
156+
// actualOrder[index] = result.Result;
157+
// await Task.Yield();
158+
// }
159+
// });
160+
161+
// // Consumer 2: Consume tasks in completion order and store the result
162+
// var c2 = Task.Run(async () =>
163+
// {
164+
// await foreach (var result in taskQueue.GetConsumingEnumerable())
165+
// {
166+
// var index = Interlocked.Increment(ref currentIndex);
167+
// actualOrder[index] = result.Result;
168+
// await Task.Yield();
169+
// }
170+
// });
171+
172+
// await Task.WhenAll(c1, c2);
173+
174+
// // Assert that the tasks were processed in the correct order
175+
// Assert.Equal(expectedOrder, actualOrder);
176+
// }
177+
178+
// private async Task<string> SimulateTask(string name, int delay)
179+
// {
180+
// //Thread.Sleep(delay);
181+
// await Task.Delay(delay); // Simulate work
182+
// return name;
183+
// }
184+
//}

0 commit comments

Comments
 (0)