@@ -24,13 +24,14 @@ import (
24
24
"github.com/hyperledger/fabric/common/ledger/testutil"
25
25
"github.com/hyperledger/fabric/core/ledger"
26
26
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
27
- "github.com/hyperledger/fabric/core/ledger/ledgerconfig"
28
27
)
29
28
30
29
func TestTxSimulatorWithNoExistingData (t * testing.T ) {
30
+ // run the tests for each environment configured in pkg_test.go
31
31
for _ , testEnv := range testEnvs {
32
32
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
33
- testEnv .init (t )
33
+ testLedgerID := "testtxsimulatorwithnoexistingdata"
34
+ testEnv .init (t , testLedgerID )
34
35
testTxSimulatorWithNoExistingData (t , testEnv )
35
36
testEnv .cleanup ()
36
37
}
@@ -55,7 +56,8 @@ func testTxSimulatorWithNoExistingData(t *testing.T, env testEnv) {
55
56
func TestTxSimulatorWithExistingData (t * testing.T ) {
56
57
for _ , testEnv := range testEnvs {
57
58
t .Run (testEnv .getName (), func (t * testing.T ) {
58
- testEnv .init (t )
59
+ testLedgerID := "testtxsimulatorwithexistingdata"
60
+ testEnv .init (t , testLedgerID )
59
61
testTxSimulatorWithExistingData (t , testEnv )
60
62
testEnv .cleanup ()
61
63
})
@@ -99,16 +101,16 @@ func testTxSimulatorWithExistingData(t *testing.T, env testEnv) {
99
101
100
102
// verify the versions of keys in persistence
101
103
vv , _ := env .getVDB ().GetState ("ns1" , "key1" )
102
- //TODO re-enable after adding couch version wrapper
103
- //testutil.AssertEquals(t, vv.Version, version.NewHeight(2, 1))
104
+ testutil .AssertEquals (t , vv .Version , version .NewHeight (1 , 1 ))
104
105
vv , _ = env .getVDB ().GetState ("ns1" , "key2" )
105
106
testutil .AssertEquals (t , vv .Version , version .NewHeight (0 , 1 ))
106
107
}
107
108
108
109
func TestTxValidation (t * testing.T ) {
109
110
for _ , testEnv := range testEnvs {
110
111
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
111
- testEnv .init (t )
112
+ testLedgerID := "testtxvalidation"
113
+ testEnv .init (t , testLedgerID )
112
114
testTxValidation (t , testEnv )
113
115
testEnv .cleanup ()
114
116
}
@@ -128,7 +130,8 @@ func testTxValidation(t *testing.T, env testEnv) {
128
130
txRWSet1 , _ := s1 .GetTxSimulationResults ()
129
131
txMgrHelper .validateAndCommitRWSet (txRWSet1 )
130
132
131
- // simulate tx2 that make changes to existing data
133
+ // simulate tx2 that make changes to existing data.
134
+ // tx2: Read/Update ns1:key1, Delete ns2:key3.
132
135
s2 , _ := txMgr .NewTxSimulator ()
133
136
value , _ := s2 .GetState ("ns1" , "key1" )
134
137
testutil .AssertEquals (t , value , []byte ("value1" ))
@@ -137,55 +140,64 @@ func testTxValidation(t *testing.T, env testEnv) {
137
140
s2 .DeleteState ("ns2" , "key3" )
138
141
s2 .Done ()
139
142
140
- // simulate tx3 before committing tx2 changes. Reads and modifies the key changed by tx2
143
+ // simulate tx3 before committing tx2 changes. Reads and modifies the key changed by tx2.
144
+ // tx3: Read/Update ns1:key1
141
145
s3 , _ := txMgr .NewTxSimulator ()
142
146
s3 .GetState ("ns1" , "key1" )
143
147
s3 .SetState ("ns1" , "key1" , []byte ("value1_3" ))
144
148
s3 .Done ()
145
149
146
150
// simulate tx4 before committing tx2 changes. Reads and Deletes the key changed by tx2
151
+ // tx4: Read/Delete ns2:key3
147
152
s4 , _ := txMgr .NewTxSimulator ()
148
153
s4 .GetState ("ns2" , "key3" )
149
154
s4 .DeleteState ("ns2" , "key3" )
150
155
s4 .Done ()
151
156
152
157
// simulate tx5 before committing tx2 changes. Modifies and then Reads the key changed by tx2 and writes a new key
158
+ // tx5: Update/Read ns1:key1
153
159
s5 , _ := txMgr .NewTxSimulator ()
154
160
s5 .SetState ("ns1" , "key1" , []byte ("new_value" ))
155
161
s5 .GetState ("ns1" , "key1" )
156
162
s5 .Done ()
157
163
158
164
// simulate tx6 before committing tx2 changes. Only writes a new key, does not reads/writes a key changed by tx2
165
+ // tx6: Update ns1:new_key
159
166
s6 , _ := txMgr .NewTxSimulator ()
160
167
s6 .SetState ("ns1" , "new_key" , []byte ("new_value" ))
161
168
s6 .Done ()
162
169
170
+ // Summary of simulated transactions
171
+ // tx2: Read/Update ns1:key1, Delete ns2:key3.
172
+ // tx3: Read/Update ns1:key1
173
+ // tx4: Read/Delete ns2:key3
174
+ // tx5: Update/Read ns1:key1
175
+ // tx6: Update ns1:new_key
176
+
163
177
// validate and commit RWset for tx2
164
178
txRWSet2 , _ := s2 .GetTxSimulationResults ()
165
179
txMgrHelper .validateAndCommitRWSet (txRWSet2 )
166
- //TODO re-enable after adding couch version wrapper
167
- /*
168
- //RWSet for tx3 and tx4 should not be invalid now
169
- txRWSet3, _ := s3.GetTxSimulationResults()
170
- txMgrHelper.checkRWsetInvalid(txRWSet3)
171
-
172
- txRWSet4, _ := s4.GetTxSimulationResults()
173
- txMgrHelper.checkRWsetInvalid(txRWSet4)
174
-
175
- //tx5 shold still be valid as it over-writes the key first and then reads
176
- txRWSet5, _ := s5.GetTxSimulationResults()
177
- txMgrHelper.validateAndCommitRWSet(txRWSet5)
178
-
179
- // tx6 should still be valid as it only writes a new key
180
- txRWSet6, _ := s6.GetTxSimulationResults()
181
- txMgrHelper.validateAndCommitRWSet(txRWSet6)
182
- */
180
+
181
+ //RWSet for tx3 and tx4 and tx5 should be invalid now due to read conflicts
182
+ txRWSet3 , _ := s3 .GetTxSimulationResults ()
183
+ txMgrHelper .checkRWsetInvalid (txRWSet3 )
184
+
185
+ txRWSet4 , _ := s4 .GetTxSimulationResults ()
186
+ txMgrHelper .checkRWsetInvalid (txRWSet4 )
187
+
188
+ txRWSet5 , _ := s5 .GetTxSimulationResults ()
189
+ txMgrHelper .checkRWsetInvalid (txRWSet5 )
190
+
191
+ // tx6 should still be valid as it only writes a new key
192
+ txRWSet6 , _ := s6 .GetTxSimulationResults ()
193
+ txMgrHelper .validateAndCommitRWSet (txRWSet6 )
183
194
}
184
195
185
196
func TestTxPhantomValidation (t * testing.T ) {
186
197
for _ , testEnv := range testEnvs {
187
198
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
188
- testEnv .init (t )
199
+ testLedgerID := "testtxphantomvalidation"
200
+ testEnv .init (t , testLedgerID )
189
201
testTxPhantomValidation (t , testEnv )
190
202
testEnv .cleanup ()
191
203
}
@@ -254,23 +266,29 @@ func testTxPhantomValidation(t *testing.T, env testEnv) {
254
266
func TestIterator (t * testing.T ) {
255
267
for _ , testEnv := range testEnvs {
256
268
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
257
- testEnv .init (t )
269
+
270
+ testLedgerID := "testiterator_1"
271
+ testEnv .init (t , testLedgerID )
258
272
testIterator (t , testEnv , 10 , 2 , 7 )
259
273
testEnv .cleanup ()
260
274
261
- testEnv .init (t )
275
+ testLedgerID = "testiterator_2"
276
+ testEnv .init (t , testLedgerID )
262
277
testIterator (t , testEnv , 10 , 1 , 11 )
263
278
testEnv .cleanup ()
264
279
265
- testEnv .init (t )
280
+ testLedgerID = "testiterator_3"
281
+ testEnv .init (t , testLedgerID )
266
282
testIterator (t , testEnv , 10 , 0 , 0 )
267
283
testEnv .cleanup ()
268
284
269
- testEnv .init (t )
285
+ testLedgerID = "testiterator_4"
286
+ testEnv .init (t , testLedgerID )
270
287
testIterator (t , testEnv , 10 , 5 , 0 )
271
288
testEnv .cleanup ()
272
289
273
- testEnv .init (t )
290
+ testLedgerID = "testiterator_5"
291
+ testEnv .init (t , testLedgerID )
274
292
testIterator (t , testEnv , 10 , 0 , 5 )
275
293
testEnv .cleanup ()
276
294
}
@@ -337,7 +355,8 @@ func testIterator(t *testing.T, env testEnv, numKeys int, startKeyNum int, endKe
337
355
func TestIteratorWithDeletes (t * testing.T ) {
338
356
for _ , testEnv := range testEnvs {
339
357
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
340
- testEnv .init (t )
358
+ testLedgerID := "testiteratorwithdeletes"
359
+ testEnv .init (t , testLedgerID )
341
360
testIteratorWithDeletes (t , testEnv )
342
361
testEnv .cleanup ()
343
362
}
@@ -371,15 +390,16 @@ func testIteratorWithDeletes(t *testing.T, env testEnv) {
371
390
defer itr .Close ()
372
391
kv , _ := itr .Next ()
373
392
testutil .AssertEquals (t , kv .(* ledger.KV ).Key , createTestKey (3 ))
374
- //TODO re-enable after adding couch delete function
375
- // kv, _ = itr.Next()
376
- // testutil.AssertEquals(t, kv.(*ledger.KV).Key, createTestKey(5))
393
+
394
+ kv , _ = itr .Next ()
395
+ testutil .AssertEquals (t , kv .(* ledger.KV ).Key , createTestKey (5 ))
377
396
}
378
397
379
398
func TestTxValidationWithItr (t * testing.T ) {
380
399
for _ , testEnv := range testEnvs {
381
400
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
382
- testEnv .init (t )
401
+ testLedgerID := "testtxvalidationwithitr"
402
+ testEnv .init (t , testLedgerID )
383
403
testTxValidationWithItr (t , testEnv )
384
404
testEnv .cleanup ()
385
405
}
@@ -430,12 +450,9 @@ func testTxValidationWithItr(t *testing.T, env testEnv) {
430
450
txRWSet4 , _ := s4 .GetTxSimulationResults ()
431
451
txMgrHelper .validateAndCommitRWSet (txRWSet4 )
432
452
433
- //TODO re-enable after adding couch version wrapper
434
- /*
435
- //RWSet tx3 should not be invalid now
436
- txRWSet3, _ := s3.GetTxSimulationResults()
437
- txMgrHelper.checkRWsetInvalid(txRWSet3)
438
- */
453
+ //RWSet tx3 should be invalid now
454
+ txRWSet3 , _ := s3 .GetTxSimulationResults ()
455
+ txMgrHelper .checkRWsetInvalid (txRWSet3 )
439
456
440
457
// tx2 should still be valid
441
458
txRWSet2 , _ := s2 .GetTxSimulationResults ()
@@ -446,7 +463,8 @@ func testTxValidationWithItr(t *testing.T, env testEnv) {
446
463
func TestGetSetMultipeKeys (t * testing.T ) {
447
464
for _ , testEnv := range testEnvs {
448
465
t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
449
- testEnv .init (t )
466
+ testLedgerID := "testgetsetmultipekeys"
467
+ testEnv .init (t , testLedgerID )
450
468
testGetSetMultipeKeys (t , testEnv )
451
469
testEnv .cleanup ()
452
470
}
@@ -504,18 +522,15 @@ func createTestValue(i int) []byte {
504
522
//TestExecuteQueryQuery is only tested on the CouchDB testEnv
505
523
func TestExecuteQuery (t * testing.T ) {
506
524
507
- // Query is only tested on the CouchDB testEnv
508
- if ledgerconfig .IsCouchDBEnabled () == true {
509
- testEnvs = []testEnv {& couchDBLockBasedEnv {}}
510
- } else {
511
- testEnvs = []testEnv {}
512
- }
513
-
514
525
for _ , testEnv := range testEnvs {
515
- t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
516
- testEnv .init (t )
517
- testExecuteQuery (t , testEnv )
518
- testEnv .cleanup ()
526
+ // Query is only supported and tested on the CouchDB testEnv
527
+ if testEnv .getName () == couchDBtestEnvName {
528
+ t .Logf ("Running test for TestEnv = %s" , testEnv .getName ())
529
+ testLedgerID := "testexecutequery"
530
+ testEnv .init (t , testLedgerID )
531
+ testExecuteQuery (t , testEnv )
532
+ testEnv .cleanup ()
533
+ }
519
534
}
520
535
}
521
536
@@ -560,7 +575,8 @@ func testExecuteQuery(t *testing.T, env testEnv) {
560
575
queryExecuter , _ := txMgr .NewQueryExecutor ()
561
576
queryString := "{\" selector\" :{\" owner\" : {\" $eq\" : \" bob\" }},\" limit\" : 10,\" skip\" : 0}"
562
577
563
- itr , _ := queryExecuter .ExecuteQuery ("ns1" , queryString )
578
+ itr , err := queryExecuter .ExecuteQuery ("ns1" , queryString )
579
+ testutil .AssertNoError (t , err , "Error upon ExecuteQuery()" )
564
580
565
581
counter := 0
566
582
for {
0 commit comments