-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
173 lines (131 loc) · 4.39 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using Grpc.Net.Client;
using static AccumulatorOps.AccumulatorOps;
using AccumulatorOps;
using Google.Protobuf.WellKnownTypes;
using System.Reflection;
using Grpc.Core;
// The port number must match the port of the gRPC server.
using var channel = GrpcChannel.ForAddress("https://localhost:7112");
var accumulatorClientStub = new AccumulatorOpsClient(channel);
# region Tests
async Task<bool> TestPing()
{
var ping = await accumulatorClientStub.PingAsync(new Empty());
return ping.Value == "Pong";
}
// Svi testovi su pisani sa pretpostavkom da nema poziva od strane drugih klijenata za vreme njihovog trajanja.
// Ukoliko se ipak tako nesto i desi, testovi nece nece proci (vratice `false`).
async Task<bool> TestPeformOperationUnary()
{
int beforeTestingValue = (await accumulatorClientStub.GetAccumulatorValueAsync(new Empty())).Value;
var afterIncrement = await accumulatorClientStub.PerformOperationUnaryAsync(new Operation
{
Type = OperationType.Inc
});
if (afterIncrement.AccumulatorValue != beforeTestingValue + 1)
return false;
var afterDecrement = await accumulatorClientStub.PerformOperationUnaryAsync(new Operation
{
Type = OperationType.Dec
});
if (afterDecrement.AccumulatorValue != beforeTestingValue)
return false;
var afterAdding = await accumulatorClientStub.PerformOperationUnaryAsync(new Operation
{
Type = OperationType.Add,
Operand = 10,
});
if (afterAdding.AccumulatorValue != beforeTestingValue + 10)
return false;
var afterSubtracting = await accumulatorClientStub.PerformOperationUnaryAsync(new Operation
{
Type = OperationType.Add,
Operand = -10,
});
if (afterSubtracting.AccumulatorValue != beforeTestingValue)
return false;
return true;
}
async Task<bool> TestPerformOperationClientStream()
{
int beforeTestingValue = (await accumulatorClientStub.GetAccumulatorValueAsync(new Empty())).Value;
var simplex = accumulatorClientStub.PerformOperationClientStream();
await simplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Inc
});
await simplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Dec,
});
await simplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Add,
Operand = 10,
});
await simplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Add,
Operand = -10,
});
// Used to signal the end of streaming.
await simplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Nop
});
var afterTestingResponse = await simplex.ResponseAsync;
Console.WriteLine(afterTestingResponse);
if (beforeTestingValue != afterTestingResponse.AccumulatorValue)
return false;
return true;
}
async Task<bool> TestPerformOperationBiStream()
{
int beforeTestingValue = (await accumulatorClientStub.GetAccumulatorValueAsync(new Empty())).Value;
var duplex = accumulatorClientStub.PerformOperationBiStream();
var accumulatorValues = new int[] { beforeTestingValue + 1, beforeTestingValue, beforeTestingValue + 10, beforeTestingValue };
await duplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Inc
});
await duplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Dec,
});
await duplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Add,
Operand = 10,
});
await duplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Add,
Operand = -10,
});
// Used to signal the end of streaming.
await duplex.RequestStream.WriteAsync(new Operation
{
Type = OperationType.Nop
});
int i = 0;
await foreach (var response in duplex.ResponseStream.ReadAllAsync())
{
if (response.AccumulatorValue != accumulatorValues[i++])
return false;
}
return true;
}
var tests = new List<Func<Task<bool>>>
{
TestPing,
TestPeformOperationUnary,
TestPerformOperationClientStream,
TestPerformOperationBiStream,
};
#endregion Tests
#region Run tests
foreach (var test in tests)
Console.WriteLine($"Tested '{test.GetMethodInfo().Name}'. Success: {await test()}");
#endregion Run tests
Console.WriteLine("Press any key to exit...");
Console.ReadKey();