1 module tagion.testbench.dart.dart_sync_snap_back;
2 // Default import list for bdd
3 import std.algorithm : each, equal, filter, map, sort;
4 import std.file : mkdirRecurse;
5 import std.format : format;
6 import std.path : buildPath, setExtension;
7 import std.random : MinstdRand0, randomSample, randomShuffle;
8 import std.range;
9 import std.stdio;
10 import std.typecons : Tuple;
11 import tagion.Keywords;
12 import tagion.basic.basic : tempfile;
13 import tagion.basic.basic : forceRemove;
14 import tagion.behaviour;
15 import tagion.communication.HiRPC;
16 import tagion.crypto.SecureInterfaceNet : HashNet, SecureNet;
17 import tagion.dart.BlockFile : BlockFile;
18 import tagion.dart.DART : DART;
19 import tagion.dart.DARTBasic : DARTIndex, dartIndex;
20 import tagion.dart.DARTFakeNet;
21 import tagion.dart.DARTFile : DARTFile;
22 import tagion.dart.Recorder : Archive, RecordFactory;
23 import tagion.hibon.Document;
24 import tagion.hibon.HiBONJSON : toPretty;
25 import tagion.hibon.HiBONRecord;
26 import tagion.testbench.dart.dart_helper_functions;
27 import tagion.testbench.dart.dartinfo;
28 import tagion.testbench.tools.Environment;
29 import tagion.testbench.tools.Environment;
30 import tagion.utils.Random;
31 
32 enum feature = Feature(
33             "Dart snap syncing",
34             [
35             "All test in this bdd should use dart fakenet. This test covers after a archive has been removed that was in a deep rim. What then happens when syncing such a branch?"
36             ]);
37 
38 alias FeatureContext = Tuple!(
39         SyncToAnotherDb, "SyncToAnotherDb",
40         FeatureGroup*, "result"
41 );
42 
43 @safe @Scenario("Sync to another db.",
44         [])
45 class SyncToAnotherDb {
46     DART db1;
47     DART db2;
48 
49     DARTIndex[] db1_fingerprints;
50 
51     const ushort angle = 43961;
52     const ushort to = 43962;
53 
54     DartInfo info;
55 
56     this(DartInfo info) {
57         this.info = info;
58     }
59 
60     @Given("I have a dartfile with one archive.")
61     Document archive() {
62         Exception dart_exception;
63         db1 = new DART(info.net, info.dartfilename, dart_exception);
64         check(dart_exception is null, format("Failed to open DART %s", dart_exception.msg));
65 
66         const bullseye = db1.bullseye();
67 
68         // db1.dump();
69 
70         const doc = DARTFakeNet.fake_doc(info.deep_table[1]);
71         const doc_bullseye = dartIndex(info.net, doc);
72 
73         check(bullseye == doc_bullseye, "Bullseye not equal to doc");
74 
75         return result_ok;
76     }
77 
78     @Given("I have a empty dartfile2.")
79     Document dartfile2() {
80         info.dartfilename2.forceRemove;
81         DART.create(info.dartfilename2, info.net);
82         Exception dart_exception;
83         db2 = new DART(info.net, info.dartfilename2, dart_exception);
84         check(dart_exception is null, format("Failed to open DART %s", dart_exception.msg));
85 
86         return result_ok;
87     }
88 
89     @Given("I sync the databases.")
90     Document databases() {
91         syncDarts(db1, db2, angle, to);
92         // db2.dump();
93         return result_ok;
94     }
95 
96     @Then("the bullseyes should be the same.")
97     Document same() {
98         check(db1.bullseye == db2.bullseye, "Bullseyes not the same");
99         return result_ok;
100     }
101 
102     @Then("check if the data is not lost.")
103     Document lost() {
104 
105         db1.close();
106         db2.close();
107         return result_ok;
108     }
109 
110 }