Skip to content

Commit bf8c304

Browse files
shestakovgGennadii Shestakov
andauthored
OSS-676: Parallel testing (#150)
OSS-676: * Added parallel test * added extra check for other net platforms * made separate delegate for NET Core 2.1. Sometimes getting an errors, that related to HttpConection. It's a core part of framework, so I can't touch it Co-authored-by: Gennadii Shestakov <[email protected]>
1 parent ec88143 commit bf8c304

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using FaunaDB.Types;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using static FaunaDB.Query.Language;
9+
using System.Collections.Concurrent;
10+
using FaunaDB.Client;
11+
12+
namespace Test
13+
{
14+
public class ParallelTest: TestCase
15+
{
16+
private const int IN_PARALLEL = 1000;
17+
private const int MAX_ATTEMPTS = 10;
18+
private const string COLLECTION_NAME = "ParallelTestCollection";
19+
20+
[OneTimeSetUp]
21+
public void SetUpCollection()
22+
{
23+
SetUpCollectionAsync().Wait();
24+
}
25+
public async Task SetUpCollectionAsync()
26+
{
27+
var client = new FaunaClient(secret: faunaSecret, endpoint: faunaEndpoint);
28+
bool collectionExist = (await client.Query(Exists(Collection(COLLECTION_NAME)))).To<bool>().Value;
29+
30+
//create collection and fill documents
31+
if (!collectionExist)
32+
{
33+
await client.Query(CreateCollection(Obj("name", COLLECTION_NAME)));
34+
for (int i = 0; i < 10; i++)
35+
{
36+
await client.Query(
37+
Create(
38+
Collection(COLLECTION_NAME),
39+
Obj("data", FaunaDB.Types.Encoder.Encode(new SampleDocument() { Id = i+1, Text=$"Document {i+1}" }))
40+
)
41+
);
42+
}
43+
}
44+
}
45+
46+
[Test]
47+
public void ParallelQueriesTest()
48+
{
49+
Random random = new Random();
50+
ConcurrentBag<Exception> exceptions = new ConcurrentBag<Exception>();
51+
var clients = GetClientsPool();
52+
#if !NETCOREAPP2_1
53+
Func<FaunaClient, Task> query = async (client) =>
54+
{
55+
try
56+
{
57+
await client.Query(Map(Paginate(Documents(Collection(COLLECTION_NAME))), reference => Get(reference)));
58+
await client.Query(Sum(Arr(1, 2, 3.5, 0.25)));
59+
}
60+
catch (Exception e)
61+
{
62+
exceptions.Add(e);
63+
}
64+
};
65+
66+
#endif
67+
#if NETCOREAPP2_1
68+
Action<FaunaClient> queryCore21 = (client) =>
69+
{
70+
try
71+
{
72+
Task.Run(async () => await client.Query(Map(Paginate(Documents(Collection(COLLECTION_NAME))), reference => Get(reference))));
73+
Task.Run(async () => await client.Query(Sum(Arr(1, 2, 3.5, 0.25))));
74+
}
75+
catch (Exception e)
76+
{
77+
exceptions.Add(e);
78+
}
79+
};
80+
#endif
81+
for (int i = 0; i < MAX_ATTEMPTS; i++)
82+
{
83+
List<Task> tasks = new List<Task>();
84+
for (int j = 0; j < IN_PARALLEL; j++)
85+
{
86+
#if !NETCOREAPP2_1
87+
tasks.Add(Task.Run(async () => await query(clients[random.Next(0, clients.Count - 1)])));
88+
#else
89+
tasks.Add(Task.Run(() => queryCore21(clients[random.Next(0, clients.Count - 1)])));
90+
#endif
91+
}
92+
Task.WaitAll(tasks.ToArray());
93+
}
94+
95+
Assert.IsFalse(exceptions.Any(), $"Exceptions occured. Details:{Environment.NewLine}{PrintExceptions(exceptions)}");
96+
}
97+
98+
private IList<FaunaClient> GetClientsPool()
99+
{
100+
List<FaunaClient> clients = new List<FaunaClient>();
101+
for (int i = 0; i < MAX_ATTEMPTS; i++)
102+
{
103+
clients.Add(new FaunaClient(secret: faunaSecret, endpoint: faunaEndpoint));
104+
}
105+
return clients;
106+
}
107+
108+
private string PrintExceptions(IEnumerable<Exception> exceptions)
109+
{
110+
StringBuilder sb = new StringBuilder();
111+
Func<Exception, string> printException = (exception) =>
112+
$"Exception: {exception.Message}{Environment.NewLine}Stack trace:{exception.StackTrace}";
113+
foreach (Exception exception in exceptions.Take<Exception>(MAX_ATTEMPTS))
114+
{
115+
string message = printException(exception);
116+
if (exception.InnerException != null)
117+
message += $"Inner exception:{Environment.NewLine}" + printException(exception.InnerException);
118+
sb.AppendLine(message);
119+
}
120+
return sb.ToString();
121+
}
122+
123+
class SampleDocument
124+
{
125+
[FaunaField("id")]
126+
public int Id;
127+
[FaunaField("text")]
128+
public string Text;
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)