[System.Data] Try to fix random DataViewTest.DefaultColumnNameAddListChangedTest...
[mono-project.git] / mcs / tests / test-async-16.cs
blobd20aeb4efd43f1fb56af1e62f884a61a763a0d16
1 using System;
2 using System.Threading.Tasks;
3 using System.Threading;
4 using System.Reflection;
5 using System.Linq;
6 using System.Collections.Generic;
8 class Base : IDisposable
10 protected static int dispose_counter;
12 public void Dispose ()
14 ++dispose_counter;
18 class Tester : Base
20 async Task<int> SwitchTest_1 ()
22 switch (await Task.Factory.StartNew (() => "X").ConfigureAwait (false)) {
23 case "A":
24 return 1;
25 case "B":
26 return 2;
27 case "C":
28 return 3;
29 case "D":
30 return 4;
31 case "X":
32 return 0;
35 return 5;
38 async Task<int> Using_1 ()
40 using (Base a = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false),
41 b = await Task.Factory.StartNew (() => new Tester ()).ConfigureAwait (false),
42 c = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false),
43 d = await Task.Factory.StartNew (() => new Base ()).ConfigureAwait (false)) {
46 if (dispose_counter != 4)
47 return 1;
49 return 0;
52 async Task<int> Foreach_1 ()
54 int total = 0;
55 foreach (var e in await Task.Factory.StartNew (() => new List<int> () { 1, 2, 3 }).ConfigureAwait (false)) {
56 total += e;
59 return total - 6;
62 static bool RunTest (MethodInfo test)
64 Console.Write ("Running test {0, -25}", test.Name);
65 try {
66 Task t = test.Invoke (new Tester (), null) as Task;
67 if (!Task.WaitAll (new[] { t }, 1000)) {
68 Console.WriteLine ("FAILED (Timeout)");
69 return false;
72 var ti = t as Task<int>;
73 if (ti != null) {
74 if (ti.Result != 0) {
75 Console.WriteLine ("FAILED (Result={0})", ti.Result);
76 return false;
78 } else {
79 var tb = t as Task<bool>;
80 if (tb != null) {
81 if (!tb.Result) {
82 Console.WriteLine ("FAILED (Result={0})", tb.Result);
83 return false;
88 Console.WriteLine ("OK");
89 return true;
90 } catch (Exception e) {
91 Console.WriteLine ("FAILED");
92 Console.WriteLine (e.ToString ());
93 return false;
97 public static int Main ()
99 var tests = from test in typeof (Tester).GetMethods (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)
100 where test.GetParameters ().Length == 0
101 orderby test.Name
102 select RunTest (test);
104 int failures = tests.Count (a => !a);
105 Console.WriteLine (failures + " tests failed");
106 return failures;