-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathServerEventsClientTest.cs
More file actions
188 lines (160 loc) · 6.64 KB
/
ServerEventsClientTest.cs
File metadata and controls
188 lines (160 loc) · 6.64 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Casper.Network.SDK;
using Casper.Network.SDK.SSE;
using Casper.Network.SDK.Types;
using NUnit.Framework;
namespace NetCasperTest
{
[Category("NCTL-SSE")]
public class ServerEventsClientTest
{
private string _nodeAddress;
private string _chainName = "casper-net-1";
private NetCasperClient _client;
private KeyPair _faucetKey;
private string nodeIpSSE;
private int nodePortSSE;
private string nodeUriSSE;
[SetUp]
public void Setup()
{
_nodeAddress = Environment.GetEnvironmentVariable("CASPERNETSDK_NODE_ADDRESS");
Assert.IsNotNull(_nodeAddress,
"Please, set environment variable CASPERNETSDK_NODE_ADDRESS with a valid node url (with port).");
_client = new NetCasperClient(_nodeAddress);
var fkFilename = TestContext.CurrentContext.TestDirectory +
"/TestData/faucetact.pem";
_faucetKey = KeyPair.FromPem(fkFilename);
Assert.IsNotNull(_faucetKey, $"Cannot read faucet key from '{fkFilename}");
nodeIpSSE = Environment.GetEnvironmentVariable("CASPERNETSDK_NODE_SSE_IP");
Assert.IsNotNull(nodeIpSSE,
"Please, set environment variable CASPERNETSDK_NODE_SSE_IP with a valid node ip.");
var port = Environment.GetEnvironmentVariable("CASPERNETSDK_NODE_SSE_PORT");
Assert.IsNotNull(port,
"Please, set environment variable CASPERNETSDK_NODE_SSE_PORT with a valid node ip.");
Assert.IsTrue(int.TryParse(port, out nodePortSSE));
nodeUriSSE = Environment.GetEnvironmentVariable("CASPERNETSDK_NODE_SSE_URI");
Assert.IsNotNull(nodeUriSSE,
"Please, set environment variable CASPERNETSDK_NODE_SSE_URI with a valid node sse uri.");
}
[Test]
public void ListenBlocksAdded()
{
int nBlocks = 0;
var sse = new ServerEventsClient(nodeIpSSE, nodePortSSE);
sse.AddEventCallback(EventType.BlockAdded, "catch-blocks-cb",
(SSEvent evt) =>
{
try
{
if (evt.EventType == EventType.BlockAdded)
{
var block = evt.Parse<BlockAdded>();
Assert.IsNotNull(block.BlockHash);
nBlocks++;
}
else
{
Assert.Fail($"No event different from BlockAdded expected. Received: '{evt.EventType}'");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
},
startFrom: 0);
sse.StartListening();
int retries = 0;
while(nBlocks < 3 && retries++ < 5)
Thread.Sleep(5000);
sse.StopListening().Wait();
Assert.IsTrue(nBlocks >= 3);
}
private async Task MakeTransfer()
{
KeyPair myAccount = KeyPair.CreateNew(KeyAlgo.SECP256K1);
var deploy = DeployTemplates.StandardTransfer(
_faucetKey.PublicKey,
myAccount.PublicKey,
2500_000_000_000,
100_000_000,
_chainName);
deploy.Sign(_faucetKey);
await _client.PutDeploy(deploy);
var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(120));
var getResponse = await _client.GetDeploy(deploy.Hash, tokenSource.Token);
var execInfo = getResponse.Parse().ExecutionInfo;
var execResult = (ExecutionResultV1)execInfo.ExecutionResult;
Assert.IsTrue(execResult.IsSuccess);
}
[Test]
public async Task SSEListen()
{
int nBlocks = 0;
int nSignatures = 0;
int nApiVersion = 0;
int nDeployAccepted = 0;
int nDeployProcessed = 0;
var sse = new ServerEventsClient(nodeUriSSE);
sse.AddEventCallback(EventType.All, "catch-all-cb",
(SSEvent evt) =>
{
try
{
if (evt.EventType == EventType.FinalitySignature)
{
var sig = evt.Parse<FinalitySignature>();
Assert.IsNotNull(sig.BlockHash);
nSignatures++;
}
else if (evt.EventType == EventType.BlockAdded)
{
var block = evt.Parse<BlockAdded>();
Assert.IsNotNull(block.BlockHash);
nBlocks++;
}
else if (evt.EventType == EventType.DeployAccepted)
{
var deploy = evt.Parse<DeployAccepted>();
Assert.IsNotNull(deploy.Hash);
nDeployAccepted++;
}
else if (evt.EventType == EventType.DeployProcessed)
{
var deploy = evt.Parse<DeployProcessed>();
Assert.IsNotNull(deploy.BlockHash);
nDeployProcessed++;
}
else if (evt.EventType == EventType.ApiVersion)
{
nApiVersion++;
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
},
startFrom: 0);
sse.StartListening();
// send a deploy to generate DeployAccepted and DeployProcessed events
//
await MakeTransfer();
int retries = 0;
while(nDeployProcessed == 0 && retries++ < 3)
Thread.Sleep(5000);
await sse.StopListening();
Assert.IsTrue(nApiVersion > 0);
Assert.IsTrue(nBlocks > 0);
Assert.IsTrue(nSignatures > 0);
Assert.IsTrue(nDeployAccepted > 0);
Assert.IsTrue(nDeployProcessed > 0);
}
}
}