@@ -60,6 +60,8 @@ func TestLedgerProvider(t *testing.T) {
6060 testutil .AssertEquals (t , ledgerIds [i ], constructTestLedgerID (i ))
6161 }
6262 for i := 0 ; i < numLedgers ; i ++ {
63+ status , _ := provider .Exists (constructTestLedgerID (i ))
64+ testutil .AssertEquals (t , status , true )
6365 ledger , err := provider .Open (constructTestLedgerID (i ))
6466 testutil .AssertNoError (t , err , "" )
6567 bcInfo , err := ledger .GetBlockchainInfo ()
@@ -71,10 +73,55 @@ func TestLedgerProvider(t *testing.T) {
7173 _ , err = provider .Create (gb )
7274 testutil .AssertEquals (t , err , ErrLedgerIDExists )
7375
76+ status , err := provider .Exists (constructTestLedgerID (numLedgers ))
77+ testutil .AssertNoError (t , err , "Failed to check for ledger existence" )
78+ testutil .AssertEquals (t , false , status )
79+
7480 _ , err = provider .Open (constructTestLedgerID (numLedgers ))
7581 testutil .AssertEquals (t , err , ErrNonExistingLedgerID )
7682}
7783
84+ func TestRecovery (t * testing.T ) {
85+ env := newTestEnv (t )
86+ defer env .cleanup ()
87+ provider , _ := NewProvider ()
88+
89+ // now create the genesis block
90+ genesisBlock , _ := configtxtest .MakeGenesisBlock (constructTestLedgerID (1 ))
91+ ledger , err := provider .(* Provider ).openInternal (constructTestLedgerID (1 ))
92+ ledger .Commit (genesisBlock )
93+ ledger .Close ()
94+
95+ // Case 1: assume a crash happens, force underconstruction flag to be set to simulate
96+ // a failure where ledgerid is being created - ie., block is written but flag is not unset
97+ provider .(* Provider ).idStore .setUnderConstructionFlag (constructTestLedgerID (1 ))
98+ provider .Close ()
99+
100+ // construct a new provider to invoke recovery
101+ provider , err = NewProvider ()
102+ testutil .AssertNoError (t , err , "Provider failed to recover an underConstructionLedger" )
103+ // verify the underecoveryflag and open the ledger
104+ flag , err := provider .(* Provider ).idStore .getUnderConstructionFlag ()
105+ testutil .AssertNoError (t , err , "Failed to read the underconstruction flag" )
106+ testutil .AssertEquals (t , flag , "" )
107+ ledger , err = provider .Open (constructTestLedgerID (1 ))
108+ testutil .AssertNoError (t , err , "Failed to open the ledger" )
109+ ledger .Close ()
110+
111+ // Case 0: assume a crash happens before the genesis block of ledger 2 is comitted
112+ // Open the ID store (inventory of chainIds/ledgerIds)
113+ provider .(* Provider ).idStore .setUnderConstructionFlag (constructTestLedgerID (2 ))
114+ provider .Close ()
115+
116+ // construct a new provider to invoke recovery
117+ provider , err = NewProvider ()
118+ testutil .AssertNoError (t , err , "Provider failed to recover an underConstructionLedger" )
119+ flag , err = provider .(* Provider ).idStore .getUnderConstructionFlag ()
120+ testutil .AssertNoError (t , err , "Failed to read the underconstruction flag" )
121+ testutil .AssertEquals (t , flag , "" )
122+
123+ }
124+
78125func TestMultipleLedgerBasicRW (t * testing.T ) {
79126 env := newTestEnv (t )
80127 defer env .cleanup ()
0 commit comments