-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkVariableCollectionsTests.cs
More file actions
3407 lines (2975 loc) · 175 KB
/
NetworkVariableCollectionsTests.cs
File metadata and controls
3407 lines (2975 loc) · 175 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.TestTools;
using Random = UnityEngine.Random;
namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Client-Server only test
/// Validates using managed collections with NetworkVariable.
/// Managed Collections Tested:
/// - List
/// - Dictionary
/// - HashSet
/// This also does some testing on nested collections, but does
/// not test every possible combination.
/// </summary>
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
internal class NetworkVariableCollectionsTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 2;
private bool m_EnableDebug;
public NetworkVariableCollectionsTests(HostOrServer hostOrServer) : base(hostOrServer)
{
m_EnableDebug = false;
}
protected override bool OnSetVerboseDebug()
{
return m_EnableDebug;
}
protected override IEnumerator OnSetup()
{
ListTestHelperInt.ResetState();
ListTestHelperListInt.ResetState();
ListTestHelperSerializableObject.ResetState();
ListTestHelperListSerializableObject.ResetState();
DictionaryTestHelper.ResetState();
NestedDictionaryTestHelper.ResetState();
HashSetBaseTypeTestHelper.ResetState();
return base.OnSetup();
}
private void AddPlayerComponent<T>() where T : ListTestHelperBase
{
var component = m_PlayerPrefab.AddComponent<T>();
component.SetDebugMode(m_EnableDebug);
}
protected override void OnCreatePlayerPrefab()
{
AddPlayerComponent<ListTestHelperInt>();
AddPlayerComponent<ListTestHelperListInt>();
AddPlayerComponent<ListTestHelperSerializableObject>();
AddPlayerComponent<ListTestHelperListSerializableObject>();
AddPlayerComponent<DictionaryTestHelper>();
AddPlayerComponent<NestedDictionaryTestHelper>();
AddPlayerComponent<HashSetBaseTypeTestHelper>();
base.OnCreatePlayerPrefab();
}
private List<int> GetRandomIntList(int count)
{
var list = new List<int>();
for (int i = 0; i < count; i++)
{
list.Add(Random.Range(int.MinValue, int.MaxValue));
}
return list;
}
[UnityTest]
public IEnumerator TestListBuiltInTypeCollections()
{
var compInt = (ListTestHelperInt)null;
var compListInt = (ListTestHelperListInt)null;
var compIntServer = (ListTestHelperInt)null;
var compListIntServer = (ListTestHelperListInt)null;
var clientList = m_ClientNetworkManagers.ToList();
if (m_ServerNetworkManager.IsHost)
{
clientList.Insert(0, m_ServerNetworkManager);
}
foreach (var client in clientList)
{
///////////////////////////////////////////////////////////////////////////
// List<int> Single dimension list
compInt = client.LocalClient.PlayerObject.GetComponent<ListTestHelperInt>();
compIntServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<ListTestHelperInt>();
yield return WaitForConditionOrTimeOut(() => compInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compIntServer.name} component match!");
var randomInt = Random.Range(int.MinValue, int.MaxValue);
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
//////////////////////////////////
// No Write Owner Add Int
compIntServer.Add(randomInt, ListTestHelperBase.Targets.Owner);
}
//////////////////////////////////
// Owner Add int
compInt.Add(randomInt, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
//////////////////////////////////
// No Write Server Add Int
compInt.Add(randomInt, ListTestHelperBase.Targets.Server);
}
//////////////////////////////////
// Server Add int
compIntServer.Add(randomInt, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
//////////////////////////////////
// Owner Remove int
var index = Random.Range(0, compInt.ListCollectionOwner.Value.Count - 1);
var valueIntRemove = compInt.ListCollectionOwner.Value[index];
compInt.Remove(valueIntRemove, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
//////////////////////////////////
// Server Remove int
compIntServer.Remove(valueIntRemove, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server remove failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
yield return WaitForConditionOrTimeOut(() => compInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compIntServer.name} component match!");
////////////////////////////////////
// Owner Change int
var valueIntChange = Random.Range(int.MinValue, int.MaxValue);
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// No Write Server Change int with IsDirty restore
compIntServer.ListCollectionOwner.Value[index] = valueIntChange;
compIntServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server change failed to restore on {nameof(ListTestHelperInt)} {compInt.name}!");
// No Write Server Change int with owner state update override
compIntServer.ListCollectionOwner.Value[index] = valueIntChange;
}
compInt.ListCollectionOwner.Value[index] = valueIntChange;
compInt.ListCollectionOwner.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} change failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
//////////////////////////////////
// Server Change int
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// No Write Client Change int with IsDirty restore
compInt.ListCollectionServer.Value[index] = valueIntChange;
compInt.ListCollectionServer.IsDirty();
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client-{client.LocalClientId} change failed to restore on {nameof(ListTestHelperInt)} {compInt.name}!");
// No Write Client Change int with owner state update override
compInt.ListCollectionServer.Value[index] = valueIntChange;
}
compIntServer.ListCollectionServer.Value[index] = valueIntChange;
compIntServer.ListCollectionServer.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server change failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
////////////////////////////////////
// Owner Add Range
compInt.AddRange(GetRandomIntList(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add range failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
//////////////////////////////////
// Server Add Range
compIntServer.AddRange(GetRandomIntList(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add range failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
yield return WaitForConditionOrTimeOut(() => compInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compIntServer.name} component match!");
////////////////////////////////////
// Owner Full Set
compInt.FullSet(GetRandomIntList(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} full set failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
//////////////////////////////////
// Server Full Set
compIntServer.FullSet(GetRandomIntList(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server full set failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
////////////////////////////////////
// Owner Clear
compInt.Clear(ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} clear failed to synchronize on {nameof(ListTestHelperInt)} {compInt.name}!");
//////////////////////////////////
// Server Clear
compIntServer.Clear(ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server clear failed to synchronize on {nameof(ListTestHelperInt)} {compIntServer.name}!");
yield return WaitForConditionOrTimeOut(() => compInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compIntServer.name} component match!");
///////////////////////////////////////////////////////////////////////////
// List<List<int>> Nested List Validation
compListInt = client.LocalClient.PlayerObject.GetComponent<ListTestHelperListInt>();
compListIntServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<ListTestHelperListInt>();
yield return WaitForConditionOrTimeOut(() => compListInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListInt.name} component match! {compListInt.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListIntServer.name} component match! {compListIntServer.GetLog()}");
//////////////////////////////////
// Owner Add List<int> item
compListInt.Add(GetRandomIntList(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Add List<int> item
compListIntServer.Add(GetRandomIntList(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
//////////////////////////////////
// Owner Remove List<int> item
index = Random.Range(0, compListInt.ListCollectionOwner.Value.Count - 1);
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
compListIntServer.ListCollectionOwner.Value.Remove(compListIntServer.ListCollectionOwner.Value[index]);
compListIntServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server remove failed to restore on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
// No Write Server Remove List<int> item with update restore
compListIntServer.ListCollectionOwner.Value.Remove(compListIntServer.ListCollectionOwner.Value[index]);
}
compListInt.Remove(compListInt.ListCollectionOwner.Value[index], ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Remove List<int> item
index = Random.Range(0, compListIntServer.ListCollectionServer.Value.Count - 1);
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// No Write Client Remove List<int> item with CheckDirtyState restore
compListInt.Remove(compListInt.ListCollectionServer.Value[index], ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to restore on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
// No Write Client Remove List<int> item with update restore
compListInt.Remove(compListInt.ListCollectionServer.Value[index], ListTestHelperBase.Targets.Server);
}
compListIntServer.Remove(compListIntServer.ListCollectionServer.Value[index], ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server remove failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListInt.name} component match! {compListInt.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListIntServer.name} component match! {compListIntServer.GetLog()}");
////////////////////////////////////
// Owner Change List<int> item
index = Random.Range(0, compListInt.ListCollectionOwner.Value.Count - 1);
compListInt.ListCollectionOwner.Value[index] = GetRandomIntList(5);
compListInt.ListCollectionOwner.CheckDirtyState();
Assert.True(compListInt.ListCollectionOwner.IsDirty(), "Client Should be dirty!");
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} change index ({index}) failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Change List<int> item
index = Random.Range(0, compListIntServer.ListCollectionServer.Value.Count - 1);
compListIntServer.ListCollectionServer.Value[index] = GetRandomIntList(5);
compListIntServer.ListCollectionServer.CheckDirtyState();
Assert.True(compListIntServer.ListCollectionServer.IsDirty(), "Server Should be dirty!");
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server change failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
////////////////////////////////////
// Owner Add Range of List<int> items
var randomintListOfList = new List<List<int>>();
for (int i = 0; i < 5; i++)
{
randomintListOfList.Add(GetRandomIntList(5));
}
compListInt.AddRange(randomintListOfList, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add range failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Add Range of List<int> items
randomintListOfList = new List<List<int>>();
for (int i = 0; i < 5; i++)
{
randomintListOfList.Add(GetRandomIntList(5));
}
compListIntServer.AddRange(randomintListOfList, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add range failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compListIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListIntServer.name} component match!");
////////////////////////////////////
// Owner Full Set List<List<int>>
randomintListOfList = new List<List<int>>();
for (int i = 0; i < 5; i++)
{
randomintListOfList.Add(GetRandomIntList(5));
}
compListInt.FullSet(randomintListOfList, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} full set failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Full Set List<List<int>>
randomintListOfList = new List<List<int>>();
for (int i = 0; i < 5; i++)
{
randomintListOfList.Add(GetRandomIntList(5));
}
compListIntServer.FullSet(randomintListOfList, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server full set failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
////////////////////////////////////
// Owner Clear List<List<int>>
compListInt.Clear(ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListInt.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} clear failed to synchronize on {nameof(ListTestHelperListInt)} {compListInt.name}! {compListInt.GetLog()}");
//////////////////////////////////
// Server Clear List<List<int>>
compListIntServer.Clear(ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListIntServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server clear failed to synchronize on {nameof(ListTestHelperListInt)} {compListIntServer.name}! {compListIntServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListInt.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListInt.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListInt.name} component match!");
yield return WaitForConditionOrTimeOut(() => compListIntServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListIntServer.OwnerClientId}'s {nameof(ListTestHelperInt)} {compListIntServer.name} component match!");
}
}
[UnityTest]
public IEnumerator TestListSerializableObjectCollections()
{
var compObject = (ListTestHelperSerializableObject)null;
var compObjectServer = (ListTestHelperSerializableObject)null;
var compListObject = (ListTestHelperListSerializableObject)null;
var compListObjectServer = (ListTestHelperListSerializableObject)null;
var clientList = m_ClientNetworkManagers.ToList();
if (m_ServerNetworkManager.IsHost)
{
clientList.Insert(0, m_ServerNetworkManager);
}
foreach (var client in clientList)
{
///////////////////////////////////////////////////////////////////////////
// List<SerializableObject> Single dimension list
compObject = client.LocalClient.PlayerObject.GetComponent<ListTestHelperSerializableObject>();
compObjectServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<ListTestHelperSerializableObject>();
yield return WaitForConditionOrTimeOut(() => compObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compObject.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compObjectServer.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObjectServer.name} component match!");
//////////////////////////////////
// Owner Add SerializableObject
compObject.Add(SerializableObject.GetRandomObject(), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Add SerializableObject
compObjectServer.Add(SerializableObject.GetRandomObject(), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
//////////////////////////////////
// Owner Remove SerializableObject
var index = Random.Range(0, compObject.ListCollectionOwner.Value.Count - 1);
var valueIntRemove = compObject.ListCollectionOwner.Value[index];
compObject.Remove(valueIntRemove, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Remove SerializableObject
compObjectServer.Remove(valueIntRemove, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server remove failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
yield return WaitForConditionOrTimeOut(() => compObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compObject.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compObjectServer.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObjectServer.name} component match!");
////////////////////////////////////
// Owner Change SerializableObject
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// No Write Server Remove Serializable item with IsDirty restore
compObjectServer.ListCollectionOwner.Value[index] = SerializableObject.GetRandomObject();
compObjectServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server change failed to restore on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
// No Write Server Remove Serializable item with owner state update restore
compObjectServer.ListCollectionOwner.Value[index] = SerializableObject.GetRandomObject();
}
compObject.ListCollectionOwner.Value[index] = SerializableObject.GetRandomObject();
compObject.ListCollectionOwner.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} change failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Change SerializableObject
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// No Write Client Remove Serializable item with IsDirty restore
compObject.ListCollectionServer.Value[index] = SerializableObject.GetRandomObject();
compObject.ListCollectionServer.IsDirty();
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client-{client.LocalClientId} change failed to restore on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
// No Write Client Remove Serializable item with owner state update restore
compObject.ListCollectionServer.Value[index] = SerializableObject.GetRandomObject();
}
compObjectServer.ListCollectionServer.Value[index] = SerializableObject.GetRandomObject();
compObjectServer.ListCollectionServer.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server change failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
////////////////////////////////////
// Owner Add Range SerializableObjects
compObject.AddRange(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add range failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Add Range SerializableObjects
compObjectServer.AddRange(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add range failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
yield return WaitForConditionOrTimeOut(() => compObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compObject.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compObjectServer.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObjectServer.name} component match!");
////////////////////////////////////
// Owner Full Set SerializableObjects
compObject.FullSet(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} full set failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Full Set SerializableObjects
compObjectServer.FullSet(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server full set failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
////////////////////////////////////
// Owner Clear
compObject.Clear(ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} clear failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObject.name}!");
//////////////////////////////////
// Server Clear
compObjectServer.Clear(ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server clear failed to synchronize on {nameof(ListTestHelperSerializableObject)} {compObjectServer.name}!");
yield return WaitForConditionOrTimeOut(() => compObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compObject.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compObjectServer.OwnerClientId}'s {nameof(ListTestHelperSerializableObject)} {compObjectServer.name} component match!");
///////////////////////////////////////////////////////////////////////////
// List<List<INetworkSerializable>> Nested List Validation
compListObject = client.LocalClient.PlayerObject.GetComponent<ListTestHelperListSerializableObject>();
compListObjectServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<ListTestHelperListSerializableObject>();
yield return WaitForConditionOrTimeOut(() => compListObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListObject.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObject.name} component match! {compListObject.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListObjectServer.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name} component match! {compListObjectServer.GetLog()}");
//////////////////////////////////
// Owner Add List<INetworkSerializable> item
compListObject.Add(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}! {compListObject.GetLog()}");
//////////////////////////////////
// Server Add List<INetworkSerializable> item
compListObjectServer.Add(SerializableObject.GetListOfRandomObjects(5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}! {compListObjectServer.GetLog()}");
//////////////////////////////////
// Owner Remove List<INetworkSerializable> item
index = Random.Range(0, compListObject.ListCollectionOwner.Value.Count - 1);
compListObject.Remove(compListObject.ListCollectionOwner.Value[index], ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}! {compListObject.GetLog()}");
//////////////////////////////////
// Server Remove List<INetworkSerializable> item
index = Random.Range(0, compListObjectServer.ListCollectionServer.Value.Count - 1);
compListObjectServer.Remove(compListObjectServer.ListCollectionServer.Value[index], ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server remove failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}! {compListObjectServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListObject.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObject.name} component match! {compListObject.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListObjectServer.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name} component match! {compListObjectServer.GetLog()}");
////////////////////////////////////
// Owner Change List<INetworkSerializable> item
index = Random.Range(0, compListObject.ListCollectionOwner.Value.Count - 1);
compListObject.ListCollectionOwner.Value[index] = SerializableObject.GetListOfRandomObjects(5);
compListObject.ListCollectionOwner.CheckDirtyState();
Assert.True(compListObject.ListCollectionOwner.IsDirty(), "Client Should be dirty!");
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} change index ({index}) failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}! {compListObject.GetLog()}");
//////////////////////////////////
// Server Change List<INetworkSerializable> item
index = Random.Range(0, compListObjectServer.ListCollectionServer.Value.Count - 1);
compListObjectServer.ListCollectionServer.Value[index] = SerializableObject.GetListOfRandomObjects(5);
compListObjectServer.ListCollectionServer.CheckDirtyState();
Assert.True(compListObjectServer.ListCollectionServer.IsDirty(), "Server Should be dirty!");
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server change failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}! {compListObjectServer.GetLog()}");
////////////////////////////////////
// Owner Add Range of List<INetworkSerializable> items
compListObject.AddRange(SerializableObject.GetListOfListOfRandomObjects(5, 5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add range failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}! {compListObject.GetLog()}");
//////////////////////////////////
// Server Add Range of List<INetworkSerializable> items
compListObjectServer.AddRange(SerializableObject.GetListOfListOfRandomObjects(5, 5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add range failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}! {compListObjectServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compListObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListObject.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compListObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListObjectServer.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name} component match!");
////////////////////////////////////
// Owner Full Set List<List<INetworkSerializable>>
compListObject.FullSet(SerializableObject.GetListOfListOfRandomObjects(5, 5), ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} full set failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}!");
//////////////////////////////////
// Server Full Set List<List<INetworkSerializable>>
compListObjectServer.FullSet(SerializableObject.GetListOfListOfRandomObjects(5, 5), ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server full set failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}!");
////////////////////////////////////
// Owner Clear List<List<INetworkSerializable>>
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Server Clear List<List<INetworkSerializable>> with IsDirty restore
compListObjectServer.ListCollectionOwner.Value.Clear();
compListObjectServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server clear owner collection failed to restore back to last known valid state on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}!");
// Server Clear List<List<INetworkSerializable>> with update state restore
compListObjectServer.ListCollectionOwner.Value.Clear();
}
compListObject.Clear(ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} clear failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}!");
//////////////////////////////////
// Server Clear List<List<INetworkSerializable>>
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Client Clear List<List<INetworkSerializable>> with IsDirty restore
compListObject.ListCollectionServer.Value.Clear();
compListObject.ListCollectionServer.IsDirty();
yield return WaitForConditionOrTimeOut(() => compListObject.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client clear owner collection failed to restore back to last known valid state on {nameof(ListTestHelperListSerializableObject)} {compListObject.name}!");
// Client Clear List<List<INetworkSerializable>> with update state restore
compListObject.ListCollectionServer.Value.Clear();
}
compListObjectServer.Clear(ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compListObjectServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server clear failed to synchronize on {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name}!");
yield return WaitForConditionOrTimeOut(() => compListObject.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compListObject.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObject.name} component match!");
yield return WaitForConditionOrTimeOut(() => compListObjectServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compListObjectServer.OwnerClientId}'s {nameof(ListTestHelperListSerializableObject)} {compListObjectServer.name} component match!");
}
}
private int m_CurrentKey;
private int GetNextKey()
{
m_CurrentKey++;
return m_CurrentKey;
}
private int m_Stage;
private List<NetworkManager> m_Clients;
private bool m_IsInitialized = false;
private StringBuilder m_InitializedStatus = new StringBuilder();
private IEnumerator ValidateClients(NetworkManager clientBeingTested, bool initialize = false)
{
VerboseDebug($">>>>>>>>>>>>>>>>>>>>>>>>>[Client-{clientBeingTested.LocalClientId}][{m_Stage}][Validation]<<<<<<<<<<<<<<<<<<<<<<<<< ");
m_Stage++;
var compDictionary = (DictionaryTestHelper)null;
var compDictionaryServer = (DictionaryTestHelper)null;
var className = $"{nameof(DictionaryTestHelper)}";
var clientsInitialized = new Dictionary<ulong, bool>();
var validateTimeout = new TimeoutHelper(0.25f);
foreach (var client in m_Clients)
{
var ownerInitialized = false;
var serverInitialized = false;
///////////////////////////////////////////////////////////////////////////
// Dictionary<int, Dictionary<int,SerializableObject>> nested dictionaries
compDictionary = client.LocalClient.PlayerObject.GetComponent<DictionaryTestHelper>();
compDictionaryServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<DictionaryTestHelper>();
yield return WaitForConditionOrTimeOut(() => compDictionary.ValidateInstances(), validateTimeout);
if (initialize)
{
if (validateTimeout.HasTimedOut())
{
m_InitializedStatus.AppendLine($"[Client -{client.LocalClientId}][Owner] Failed validation: {compDictionary.GetLog()}");
}
else
{
m_InitializedStatus.AppendLine($"[Client -{client.LocalClientId}][Owner] Passed validation!");
}
ownerInitialized = !validateTimeout.HasTimedOut();
}
else
{
AssertOnTimeout($"[Owner] Not all instances of client-{compDictionary.OwnerClientId}'s {className} {compDictionary.name} component match! {compDictionary.GetLog()}");
}
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.ValidateInstances(), validateTimeout);
if (initialize)
{
if (validateTimeout.HasTimedOut())
{
m_InitializedStatus.AppendLine($"[Client -{client.LocalClientId}][Server] Failed validation: {compDictionaryServer.GetLog()}");
}
else
{
m_InitializedStatus.AppendLine($"[Client -{client.LocalClientId}][Server] Passed validation!");
}
serverInitialized = !validateTimeout.HasTimedOut();
}
else
{
AssertOnTimeout($"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");
}
if (initialize)
{
clientsInitialized.Add(client.LocalClientId, ownerInitialized && serverInitialized);
}
}
if (initialize)
{
m_IsInitialized = true;
foreach (var entry in clientsInitialized)
{
if (!entry.Value)
{
m_IsInitialized = false;
break;
}
}
}
}
private void ValidateClientsFlat(NetworkManager clientBeingTested)
{
if (!m_EnableDebug)
{
return;
}
VerboseDebug($">>>>>>>>>>>>>>>>>>>>>>>>>[{clientBeingTested.name}][{m_Stage}][Validation]<<<<<<<<<<<<<<<<<<<<<<<<< ");
m_Stage++;
var compDictionary = (DictionaryTestHelper)null;
var compDictionaryServer = (DictionaryTestHelper)null;
var className = $"{nameof(DictionaryTestHelper)}";
foreach (var client in m_Clients)
{
///////////////////////////////////////////////////////////////////////////
// Dictionary<int, Dictionary<int,SerializableObject>> nested dictionaries
compDictionary = client.LocalClient.PlayerObject.GetComponent<DictionaryTestHelper>();
compDictionaryServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<DictionaryTestHelper>();
Assert.True(compDictionary.ValidateInstances(), $"[Owner] Not all instances of client-{compDictionary.OwnerClientId}'s {className} {compDictionary.name} component match! {compDictionary.GetLog()}");
Assert.True(compDictionaryServer.ValidateInstances(), $"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");
}
}
[UnityTest]
public IEnumerator TestDictionaryCollections()
{
var compDictionary = (DictionaryTestHelper)null;
var compDictionaryServer = (DictionaryTestHelper)null;
var className = $"{nameof(DictionaryTestHelper)}";
m_Clients = m_ClientNetworkManagers.ToList();
if (m_ServerNetworkManager.IsHost)
{
m_Clients.Insert(0, m_ServerNetworkManager);
}
m_CurrentKey = 1000;
if (m_EnableDebug)
{
VerboseDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Init Values <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
foreach (var client in m_Clients)
{
compDictionary = client.LocalClient.PlayerObject.GetComponent<DictionaryTestHelper>();
compDictionary.InitValues();
compDictionaryServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<DictionaryTestHelper>();
compDictionaryServer.InitValues();
}
VerboseDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> Init Check <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
var count = 0;
while (count < 3)
{
m_InitializedStatus.Clear();
foreach (var client in m_Clients)
{
yield return ValidateClients(client, true);
}
if (m_IsInitialized)
{
break;
}
count++;
m_Stage = 0;
}
Assert.IsTrue(m_IsInitialized, $"Not all clients synchronized properly!\n {m_InitializedStatus.ToString()}");
VerboseDebug(m_InitializedStatus.ToString());
VerboseDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BEGIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
foreach (var client in m_Clients)
{
///////////////////////////////////////////////////////////////////////////
// Dictionary<int, Dictionary<int,SerializableObject>> nested dictionaries
compDictionary = client.LocalClient.PlayerObject.GetComponent<DictionaryTestHelper>();
compDictionaryServer = m_PlayerNetworkObjects[NetworkManager.ServerClientId][client.LocalClientId].GetComponent<DictionaryTestHelper>();
yield return WaitForConditionOrTimeOut(() => compDictionary.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compDictionary.OwnerClientId}'s {className} {compDictionary.name} component match! {compDictionary.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");
//////////////////////////////////
// Owner Add SerializableObject Entry
var newEntry = (GetNextKey(), SerializableObject.GetRandomObject());
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Server-side add same key and SerializableObject prior to being added to the owner side
compDictionaryServer.ListCollectionOwner.Value.Add(newEntry.Item1, newEntry.Item2);
// Checking if dirty on server side should revert back to original known current dictionary state
compDictionaryServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server add to owner write collection property failed to restore on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
// Server-side add the same key and SerializableObject to owner write permission (would throw key exists exception too if previous failed)
compDictionaryServer.ListCollectionOwner.Value.Add(newEntry.Item1, newEntry.Item2);
// Server-side add a completely new key and SerializableObject to to owner write permission property
compDictionaryServer.ListCollectionOwner.Value.Add(GetNextKey(), SerializableObject.GetRandomObject());
// Both should be overridden by the owner-side update
}
VerboseDebug($"[{compDictionary.name}][Owner] Adding Key: {newEntry.Item1}");
// Add key and SerializableObject to owner side
compDictionary.Add(newEntry, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} add failed to synchronize on {className} {compDictionary.name}! {compDictionary.GetLog()}");
ValidateClientsFlat(client);
//////////////////////////////////
// Server Add SerializableObject Entry
newEntry = (GetNextKey(), SerializableObject.GetRandomObject());
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Client-side add same key and SerializableObject to server write permission property
compDictionary.ListCollectionServer.Value.Add(newEntry.Item1, newEntry.Item2);
// Checking if dirty on client side should revert back to original known current dictionary state
compDictionary.ListCollectionServer.IsDirty();
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client-{client.LocalClientId} add to server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
// Client-side add the same key and SerializableObject to server write permission property (would throw key exists exception too if previous failed)
compDictionary.ListCollectionServer.Value.Add(newEntry.Item1, newEntry.Item2);
// Client-side add a completely new key and SerializableObject to to server write permission property
compDictionary.ListCollectionServer.Value.Add(GetNextKey(), SerializableObject.GetRandomObject());
// Both should be overridden by the server-side update
}
VerboseDebug($"[{compDictionaryServer.name}][Server] Adding Key: {newEntry.Item1}");
compDictionaryServer.Add(newEntry, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server add failed to synchronize on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
//////////////////////////////////
// Owner Remove SerializableObject Entry
var index = Random.Range(0, compDictionary.ListCollectionOwner.Value.Keys.Count - 1);
var valueInt = compDictionary.ListCollectionOwner.Value.Keys.ToList()[index];
compDictionary.Remove(valueInt, ListTestHelperBase.Targets.Owner);
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} remove failed to synchronize on {className} {compDictionary.name}! {compDictionary.GetLog()}");
//////////////////////////////////
// Server Remove SerializableObject Entry
index = Random.Range(0, compDictionary.ListCollectionServer.Value.Keys.Count - 1);
valueInt = compDictionary.ListCollectionServer.Value.Keys.ToList()[index];
compDictionaryServer.Remove(valueInt, ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server remove failed to synchronize on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compDictionary.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compDictionary.OwnerClientId}'s {className} {compDictionary.name} component match! {compDictionary.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");
ValidateClientsFlat(client);
////////////////////////////////////
// Owner Change SerializableObject Entry
var randomObject = SerializableObject.GetRandomObject();
if (compDictionary.ListCollectionOwner.Value.Keys.Count != 0)
{
if (compDictionary.ListCollectionOwner.Value.Keys.Count == 1)
{
index = 0;
valueInt = compDictionary.ListCollectionOwner.Value.Keys.ToList()[0];
}
else
{
index = Random.Range(0, compDictionary.ListCollectionOwner.Value.Keys.Count - 1);
valueInt = compDictionary.ListCollectionOwner.Value.Keys.ToList()[index];
}
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Server-side update same key value prior to being updated to the owner side
compDictionaryServer.ListCollectionOwner.Value[valueInt] = randomObject;
// Checking if dirty on server side should revert back to original known current dictionary state
compDictionaryServer.ListCollectionOwner.IsDirty();
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Server update collection entry value to local owner write collection property failed to restore on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
// Server-side update same key but with different value prior to being updated to the owner side
compDictionaryServer.ListCollectionOwner.Value[valueInt] = SerializableObject.GetRandomObject();
if (compDictionaryServer.ListCollectionOwner.Value.Keys.Count > 1)
{
// Server-side update different key with different value prior to being updated to the owner side
compDictionaryServer.ListCollectionOwner.Value[compDictionaryServer.ListCollectionOwner.Value.Keys.ToList()[(index + 1) % compDictionaryServer.ListCollectionOwner.Value.Keys.Count]] = SerializableObject.GetRandomObject();
}
// Owner-side update should force restore to current known value before updating to the owner's state update of the original index and SerializableObject
}
compDictionary.ListCollectionOwner.Value[valueInt] = randomObject;
compDictionary.ListCollectionOwner.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} change failed to synchronize on {className} {compDictionary.name}! {compDictionary.GetLog()}");
}
//////////////////////////////////
// Server Change SerializableObject
if (compDictionaryServer.ListCollectionServer.Value.Keys.Count != 0)
{
if (compDictionaryServer.ListCollectionServer.Value.Keys.Count == 1)
{
index = 0;
valueInt = compDictionaryServer.ListCollectionServer.Value.Keys.ToList()[0];
}
else
{
index = Random.Range(0, compDictionaryServer.ListCollectionServer.Value.Keys.Count - 1);
valueInt = compDictionaryServer.ListCollectionServer.Value.Keys.ToList()[index];
}
// Only test restore on non-host clients (otherwise a host is both server and client/owner)
if (!client.IsServer)
{
// Owner-side update same key value prior to being updated to the server side
compDictionary.ListCollectionServer.Value[valueInt] = randomObject;
// Checking if dirty on owner side should revert back to original known current dictionary state
compDictionary.ListCollectionServer.IsDirty();
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Client-{client.LocalClientId} update collection entry value to local server write collection property failed to restore on {className} {compDictionary.name}! {compDictionary.GetLog()}");
// Owner-side update same key but with different value prior to being updated to the server side
compDictionary.ListCollectionServer.Value[valueInt] = SerializableObject.GetRandomObject();
if (compDictionary.ListCollectionServer.Value.Keys.Count > 1)
{
// Owner-side update different key with different value prior to being updated to the server side
compDictionary.ListCollectionServer.Value[compDictionary.ListCollectionServer.Value.Keys.ToList()[(index + 1) % compDictionary.ListCollectionServer.Value.Keys.Count]] = SerializableObject.GetRandomObject();
}
// Server-side update should force restore to current known value before updating to the server's state update of the original index and SerializableObject
}
compDictionaryServer.ListCollectionServer.Value[valueInt] = SerializableObject.GetRandomObject();
compDictionaryServer.ListCollectionServer.CheckDirtyState();
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server change failed to synchronize on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
}
ValidateClientsFlat(client);
////////////////////////////////////
// Owner Clear
compDictionary.Clear(ListTestHelperBase.Targets.Owner);
VerboseDebug($"[{compDictionary.name}] Clearing dictionary..");
yield return WaitForConditionOrTimeOut(() => compDictionary.CompareTrackedChanges(ListTestHelperBase.Targets.Owner));
AssertOnTimeout($"Client-{client.LocalClientId} clear failed to synchronize on {className} {compDictionary.name}! {compDictionary.GetLog()}");
//////////////////////////////////
// Server Clear
VerboseDebug($"[{compDictionaryServer.name}] Clearing dictionary..");
compDictionaryServer.Clear(ListTestHelperBase.Targets.Server);
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.CompareTrackedChanges(ListTestHelperBase.Targets.Server));
AssertOnTimeout($"Server clear failed to synchronize on {className} {compDictionaryServer.name}! {compDictionaryServer.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compDictionary.ValidateInstances());
AssertOnTimeout($"[Owner] Not all instances of client-{compDictionary.OwnerClientId}'s {className} {compDictionary.name} component match! {compDictionary.GetLog()}");
yield return WaitForConditionOrTimeOut(() => compDictionaryServer.ValidateInstances());
AssertOnTimeout($"[Server] Not all instances of client-{compDictionaryServer.OwnerClientId}'s {className} {compDictionaryServer.name} component match! {compDictionaryServer.GetLog()}");