-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMsgBatching.cs
More file actions
279 lines (233 loc) · 11.3 KB
/
MsgBatching.cs
File metadata and controls
279 lines (233 loc) · 11.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using ProtoBuf;
using NFX;
using NFX.Parsing;
using NFX.Environment;
namespace Serbench.Specimens.Tests
{
[ProtoContract]
[DataContract]
[Serializable] public class AddressMessage
{
[ProtoMember(1)][DataMember] public string Address1;
[ProtoMember(2)][DataMember] public string Address2;
[ProtoMember(3)][DataMember] public string City;
[ProtoMember(4)][DataMember] public string State;
[ProtoMember(5)][DataMember] public string PostalCode;
[ProtoMember(6)][DataMember] public bool CanAcceptSecureShipments;
[ProtoMember(7)][DataMember] public string EMail;
[ProtoMember(8)][DataMember] public string HomePhone;
[ProtoMember(9)][DataMember] public string WorkPhone;
[ProtoMember(10)][DataMember] public string CellPhone;
[ProtoMember(11)][DataMember] public string Fax;
public static AddressMessage Build()
{
var rnd = ExternalRandomGenerator.Instance.NextRandomInteger;
return new AddressMessage
{
Address1 = NaturalTextGenerator.GenerateAddressLine(),
Address2 = (0!=(rnd & (1<<15))) ? NaturalTextGenerator.GenerateAddressLine() : null,
City = NaturalTextGenerator.GenerateCityName(),
State = NaturalTextGenerator.GenerateCityName(),
PostalCode = rnd.ToString(),
CanAcceptSecureShipments = rnd > 0,
EMail = rnd < -500000000 ? NaturalTextGenerator.GenerateEMail() : null,
HomePhone = (0!=(rnd & (1<<32))) ? "(555) 111-22234" : null,
CellPhone = (0!=(rnd & (1<<31))) ? "(555) 234-22234" : null,
Fax = (0!=(rnd & (1<<30))) ? "(555) 111-22239" : null,
};
}
}
[ProtoContract()]
[DataContract()]
[Serializable] public class SomePersonalDataMessage
{
[ProtoMember(1)][DataMember] public Guid ID;
[ProtoMember(2)][DataMember] public HumanName LegalName;
[ProtoMember(3)][DataMember] public HumanName RegistrationName;
[ProtoMember(4)][DataMember] public DateTime RegistrationDate;
[ProtoMember(5)][DataMember] public AddressMessage Residence;
[ProtoMember(6)][DataMember] public AddressMessage Shipping;
[ProtoMember(7)][DataMember] public AddressMessage Billing;
[ProtoMember(8)][DataMember] public bool? Reserved_BoolFlag1;
[ProtoMember(9)][DataMember] public bool? Reserved_BoolFlag2;
[ProtoMember(10)][DataMember] public int? Reserved_IntFlag1;
[ProtoMember(11)][DataMember] public int? Reserved_IntFlag2;
[ProtoMember(12)][DataMember] public double? Reserved_DblFlag1;
[ProtoMember(13)][DataMember] public double? Reserved_DblFlag2;
[ProtoMember(14)][DataMember] public byte[] StageAccessCode;
[ProtoMember(15)][DataMember] public byte[] SpeakerAccessCode;
[ProtoMember(16)][DataMember] public bool? RegisteredToVote;
[ProtoMember(17)][DataMember] public bool? FirearmLicense;
[ProtoMember(18)][DataMember] public bool? FishermanLicense;
[ProtoMember(19)][DataMember] public int? YearsInTheMilitary;
[ProtoMember(20)][DataMember] public int? YearsInSchool;
[ProtoMember(21)][DataMember] public int? EducationGrade;
[ProtoMember(22)][DataMember] public double? PossibleRiskFactor;
[ProtoMember(23)][DataMember] public decimal AssetsAtHand;
[ProtoMember(24)][DataMember] public decimal PotentialAssets;
[ProtoMember(25)][DataMember] public decimal TotalDebt;
[ProtoMember(26)][DataMember] public double CreditScale;
public static SomePersonalDataMessage Build()
{
var rnd = ExternalRandomGenerator.Instance.NextRandomInteger;
var primaryAddr = AddressMessage.Build();
var data = new SomePersonalDataMessage
{
ID = Guid.NewGuid(),
LegalName = HumanName.Build(),
RegistrationName = HumanName.Build(),
RegistrationDate = DateTime.Now.AddDays(-23),
Residence = primaryAddr,
Shipping = primaryAddr,
Billing = primaryAddr,
StageAccessCode = new byte[32],
SpeakerAccessCode = new byte[32],
YearsInSchool = (0!=(rnd & (1<<29))) ? 10 : (int?)null,
EducationGrade = (0!=(rnd & (1<<28))) ? 230 : (int?)null,
AssetsAtHand = 567000m,
TotalDebt = 2345m,
CreditScale = 0.02323d
};
return data;
}
}
[ProtoContract]
[DataContract]
[Serializable] public class BankMsg
{
[ProtoMember(1)][DataMember] public string FIPSCode;
[ProtoMember(2)][DataMember] public string HCFACode;
[ProtoMember(3)][DataMember] public long LANGRARCode;
[ProtoMember(4)][DataMember] public bool IsChargeable;
public static BankMsg Build()
{
var rnd = ExternalRandomGenerator.Instance.NextRandomInteger;
return new BankMsg
{
FIPSCode = NaturalTextGenerator.Generate(20),
HCFACode = NaturalTextGenerator.Generate(20),
LANGRARCode = 1239872633238L,
IsChargeable = true
};
}
}
[ProtoContract]
[DataContract]
[Serializable] public class RPCMessage
{
[ProtoMember(1)][DataMember] public Guid RequestID;
[ProtoMember(2)][DataMember] public string TypeName;
[ProtoMember(3)][DataMember] public string MethodName;
[ProtoMember(4)][DataMember] public int MethodID;
[ProtoMember(5)][DataMember] public Guid? RemoteInstance;
[ProtoMember(6)][DataMember] public double? RequiredReliability;
[ProtoMember(7)][DataMember] public bool WrapException;
[ProtoMember(8)][DataMember] public bool ElevatePermission;
//Protobuf can not do it
//http://stackoverflow.com/questions/25141791/serialize-object-with-protobuf-net
//Can not support primitives, can not support byte[]
//http://stackoverflow.com/questions/17192702/protobuf-net-serializing-system-object-with-dynamictype-throws-exception
//http://stackoverflow.com/questions/11762851/protobuf-net-a-reference-tracked-object-changed-reference-during-deserializarti
// '... A warning though: you mention "subclasses"; DynamicType does not play nicely with inheritance at the moment;
// I have some outstanding work to do there.' – Marc Gravell♦ Aug 2 '12 at 7:22
[ProtoMember(9, DynamicType=true)][DataMember] public object[] CallArguments;
public static RPCMessage Build()
{
var rnd = ExternalRandomGenerator.Instance.NextRandomInteger;
var msg = new RPCMessage
{
RequestID = Guid.NewGuid(),
TypeName = NaturalTextGenerator.Generate(80),
MethodName = NaturalTextGenerator.Generate(30),
MethodID = rnd % 25,
RemoteInstance = (0!=(rnd & (1<<32))) ? Guid.NewGuid() : (Guid?)null,
RequiredReliability = (0!=(rnd & (1<<31))) ? rnd / 100d : (double?)null,
WrapException = (0!=(rnd & (1<<30))),
ElevatePermission = (0!=(rnd & (1<<29)))
};
msg.CallArguments = new object[ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 6)];
for(var i=0; i<msg.CallArguments.Length; i++)
{
var r = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 4);
if (r==1) msg.CallArguments[i] = BankMsg.Build();
else if (r==2) msg.CallArguments[i] = NaturalTextGenerator.Generate();
else if (r==3) msg.CallArguments[i] = null; //new byte[16]; Protobuf does not support byte[] via object[]
else
msg.CallArguments[i] = AddressMessage.Build();
}
return msg;
}
}
[ProtoContract]
[DataContract]
[Serializable] public class TradingRec
{
[ProtoMember(1)][DataMember] public string Symbol;
[ProtoMember(2)][DataMember] public int Volume;
[ProtoMember(3)][DataMember] public long Bet;
[ProtoMember(4)][DataMember] public long Price;
public static TradingRec Build()
{
return new TradingRec
{
Symbol = NaturalTextGenerator.GenerateFirstName(),
Volume = ExternalRandomGenerator.Instance.NextScaledRandomInteger(-25000, 25000),
Bet = ExternalRandomGenerator.Instance.NextScaledRandomInteger(-250000, 250000) * 10000L,
Price = ExternalRandomGenerator.Instance.NextScaledRandomInteger(0, 1000000) * 10000L
};
}
}
public enum MsgBatchingType{ Personal = 0, RPC, Trading, EDI }
/// <summary>
/// This Test shows a batching scenario i.e. a full-duplex socket connection
/// when a party needs to send X consequitive atomic messages one after another in batches
/// </summary>
public class MsgBatching : Test
{
public MsgBatching(TestingSystem context, IConfigSectionNode conf)
: base(context, conf)
{
if (m_MsgCount < 1) m_MsgCount = 1;
m_Data = new List<object>(m_MsgCount);
for (var i = 0; i < m_MsgCount; i++)
m_Data.Add( m_MsgType == MsgBatchingType.Personal ? SomePersonalDataMessage.Build()
: m_MsgType== MsgBatchingType.RPC ? RPCMessage.Build()
: m_MsgType==MsgBatchingType.Trading ? (object)TradingRec.Build()
: EDI_X12_835Data.Make()
);
}
[Config]
private int m_MsgCount;
[Config]
private MsgBatchingType m_MsgType;
private List<object> m_Data = new List<object>();
public override Type GetPayloadRootType()
{
return m_MsgType== MsgBatchingType.Personal ?
typeof(SomePersonalDataMessage)
: m_MsgType== MsgBatchingType.RPC ? typeof(RPCMessage)
: m_MsgType== MsgBatchingType.Trading ? typeof(TradingRec)
: typeof(EDI_X12_835Data);
}
public override void PerformSerializationTest(Serializer serializer, Stream target)
{
foreach(var msg in m_Data)//<------ The whole point of this test is THIS LOOP that calls serialize MANY times
serializer.Serialize(msg, target);
}
public override void PerformDeserializationTest(Serializer serializer, Stream target)
{
foreach(var msg in m_Data)
{
var got = serializer.Deserialize(target);
serializer.AssertPayloadEquality(this, msg, got);
}
}
}
}