This is an automated email from the ASF dual-hosted git repository.

ptupitsyn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new ad413e3bb1a IGNITE-17710 .NET: Fix CancellationTest.TestTask (#10256)
ad413e3bb1a is described below

commit ad413e3bb1a79b0639ab6b534b722809873051a2
Author: Pavel Tupitsyn <[email protected]>
AuthorDate: Mon Sep 19 21:55:53 2022 +0300

    IGNITE-17710 .NET: Fix CancellationTest.TestTask (#10256)
    
    Use synchronization primitives instead of relying on `Thread.Sleep`.
---
 .../Compute/CancellationTest.cs                    | 56 ++++++++++++++++------
 .../dotnet/Apache.Ignite.Core.Tests/TestUtils.cs   |  2 +-
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/CancellationTest.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/CancellationTest.cs
index dbaa94b1273..653479d7942 100644
--- 
a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/CancellationTest.cs
+++ 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Compute/CancellationTest.cs
@@ -43,8 +43,23 @@ namespace Apache.Ignite.Core.Tests.Compute
         public void TestTask()
         {
             TestTask((c, t) => c.ExecuteAsync(new Task(), t));
+        }
+
+        [Test]
+        public void TestTaskWithArg()
+        {
             TestTask((c, t) => c.ExecuteAsync(new Task(), 1, t));
+        }
+
+        [Test]
+        public void TestTaskByType()
+        {
             TestTask((c, t) => c.ExecuteAsync<int, 
IList<IComputeJobResult<int>>>(typeof(Task), t));
+        }
+
+        [Test]
+        public void TestTaskByTypeWithArg()
+        {
             TestTask((c, t) => c.ExecuteAsync<object, int, 
IList<IComputeJobResult<int>>>(typeof(Task), 1, t));
         }
 
@@ -93,11 +108,25 @@ namespace Apache.Ignite.Core.Tests.Compute
 
         private void TestTask(Func<ICompute, CancellationToken, 
System.Threading.Tasks.Task> runner)
         {
-            Job.CancelCount = 0;
+            Job.Cancelled = false;
+            Job.StartEvent.Reset();
+            Job.CancelEvent.Reset();
 
-            TestClosure(runner, MillisecondsTimeout * 2);
+            using (var cts = new CancellationTokenSource())
+            {
+                var task = runner(Compute, cts.Token);
+                Assert.IsFalse(task.IsCanceled);
+
+                Job.StartEvent.Wait();
+
+                cts.Cancel();
+                TestUtils.WaitForTrueCondition(() => task.IsCanceled);
+
+                // Pass cancelled token
+                Assert.IsTrue(runner(Compute, cts.Token).IsCanceled);
+            }
 
-            Assert.IsTrue(TestUtils.WaitForCondition(() => Job.CancelCount > 
0, 5000));
+            Assert.IsTrue(TestUtils.WaitForCondition(() => Job.Cancelled, 
5000));
         }
 
         private void TestClosure(Func<ICompute, CancellationToken, 
System.Threading.Tasks.Task> runner, int delay = 0)
@@ -123,9 +152,7 @@ namespace Apache.Ignite.Core.Tests.Compute
         {
             public IDictionary<IComputeJob<int>, IClusterNode> 
Map(IList<IClusterNode> subgrid, object arg)
             {
-                return Enumerable.Range(1, 100)
-                    .SelectMany(x => subgrid)
-                    .ToDictionary(x => (IComputeJob<int>)new Job(), x => x);
+                return subgrid.Where(x => !x.IsLocal).Take(1).ToDictionary(x 
=> (IComputeJob<int>)new Job(), x => x);
             }
 
             public ComputeJobResultPolicy OnResult(IComputeJobResult<int> res, 
IList<IComputeJobResult<int>> rcvd)
@@ -143,23 +170,24 @@ namespace Apache.Ignite.Core.Tests.Compute
         [Serializable]
         private class Job : IComputeJob<int>
         {
-            private static int _cancelCount;
+            public static readonly ManualResetEventSlim StartEvent = new 
ManualResetEventSlim(false);
 
-            public static int CancelCount
-            {
-                get { return Thread.VolatileRead(ref _cancelCount); }
-                set { Thread.VolatileWrite(ref _cancelCount, value); }
-            }
+            public static readonly ManualResetEventSlim CancelEvent = new 
ManualResetEventSlim(false);
+
+            public static volatile bool Cancelled;
 
             public int Execute()
             {
-                Thread.Sleep(MillisecondsTimeout);
+                StartEvent.Set();
+                CancelEvent.Wait();
+
                 return 1;
             }
 
             public void Cancel()
             {
-                Interlocked.Increment(ref _cancelCount);
+                CancelEvent.Set();
+                Cancelled = true;
             }
         }
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/TestUtils.cs 
b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/TestUtils.cs
index 5e729bfe064..82bb79d3329 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/TestUtils.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/TestUtils.cs
@@ -78,7 +78,7 @@ namespace Apache.Ignite.Core.Tests
             {
                 "-XX:+HeapDumpOnOutOfMemoryError",
                 "-Xms2g",
-                "-Xmx6g",
+                "-Xmx4g",
                 "-ea",
                 "-DIGNITE_QUIET=true",
                 "-Duser.timezone=UTC"

Reply via email to