Skip to content

Commit

Permalink
Basic CRUD test (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Dec 4, 2024
1 parent e74e14b commit 57b96e7
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 189 deletions.
1 change: 1 addition & 0 deletions alloc/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (s *State) Run(ctx context.Context) error {
})
spawn("pump", parallel.Continue, func(ctx context.Context) error {
defer close(s.allocationPoolCh)

return s.runPump(
ctx,
s.allocationCh,
Expand Down
40 changes: 40 additions & 0 deletions alloc/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package alloc

import (
"context"
"testing"

"github.com/pkg/errors"

"github.com/outofforest/logger"
"github.com/outofforest/parallel"
)

// RunInTest creates and runs state for unit tests.
func RunInTest(t *testing.T, size, nodesPerGroup uint64) (*State, error) {
state, stateDeallocFunc, err := NewState(
size,
nodesPerGroup,
false,
)
if err != nil {
return nil, err
}
t.Cleanup(stateDeallocFunc)

ctx, cancel := context.WithCancel(logger.WithLogger(context.Background(), logger.New(logger.DefaultConfig)))
t.Cleanup(cancel)

group := parallel.NewGroup(ctx)
group.Spawn("state", parallel.Continue, state.Run)

t.Cleanup(func() {
state.Close()
group.Exit(nil)
if err := group.Wait(); err != nil && !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
})

return state, nil
}
7 changes: 2 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/outofforest/quantum"
"github.com/outofforest/quantum/alloc"
"github.com/outofforest/quantum/persistent"
"github.com/outofforest/quantum/space"
"github.com/outofforest/quantum/tx/genesis"
"github.com/outofforest/quantum/tx/transfer"
txtypes "github.com/outofforest/quantum/tx/types"
Expand Down Expand Up @@ -111,8 +110,6 @@ func BenchmarkBalanceTransfer(b *testing.B) {
panic(err)
}

st := space.NewSpaceTest(s, nil, nil, nil, nil)

hashBuff := s.NewHashBuff()
hashMatches := s.NewHashMatches()

Expand All @@ -129,7 +126,7 @@ func BenchmarkBalanceTransfer(b *testing.B) {
panic(err)
}

fmt.Println(st.Stats())
fmt.Println(s.Stats())
fmt.Println("===========================")

genesisBalance, genesisExists := s.Query(txtypes.GenesisAccount, hashBuff, hashMatches)
Expand Down Expand Up @@ -170,7 +167,7 @@ func BenchmarkBalanceTransfer(b *testing.B) {
}()

func() {
fmt.Println(st.Stats())
fmt.Println(s.Stats())

genesisBalance, genesisExists := s.Query(txtypes.GenesisAccount, hashBuff, hashMatches)
require.True(b, genesisExists)
Expand Down
41 changes: 15 additions & 26 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,19 @@ func (db *DB) deleteSnapshot(
if err != nil {
return err
}
exists, err := snapshotInfoValue.Exists(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
exists, err := snapshotInfoValue.Exists(tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}
if !exists {
return errors.Errorf("snapshot %d to delete does not exist", snapshotID)
}
snapshotInfo, err := snapshotInfoValue.Value(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
snapshotInfo, err := snapshotInfoValue.Value(tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}

if err := snapshotInfoValue.Delete(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches); err != nil {
if err := snapshotInfoValue.Delete(tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches); err != nil {
return err
}

Expand All @@ -322,16 +319,14 @@ func (db *DB) deleteSnapshot(
return err
}

exists, err = nextSnapshotInfoValue.Exists(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
exists, err = nextSnapshotInfoValue.Exists(tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}
if !exists {
return errors.Errorf("next snapshot %d does not exist", snapshotID)
}
nextSnapshotInfo, err := nextSnapshotInfoValue.Value(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
nextSnapshotInfo, err := nextSnapshotInfoValue.Value(tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}
Expand Down Expand Up @@ -375,7 +370,7 @@ func (db *DB) deleteSnapshot(
if err != nil {
return err
}
listRootAddress, err := deallocationListValue.Value(snapshotID, tx, walRecorder, allocator, deallocationHashBuff,
listRootAddress, err := deallocationListValue.Value(tx, walRecorder, allocator, deallocationHashBuff,
deallocationHashMatches)
if err != nil {
return err
Expand All @@ -387,7 +382,6 @@ func (db *DB) deleteSnapshot(
}
if listRootAddress != originalListRootAddress {
if err := deallocationListValue.Set(
snapshotID,
tx,
walRecorder,
allocator,
Expand All @@ -403,7 +397,6 @@ func (db *DB) deleteSnapshot(
nextSnapshotInfo.DeallocationRoot = snapshotInfo.DeallocationRoot
nextSnapshotInfo.PreviousSnapshotID = snapshotInfo.PreviousSnapshotID
if err := nextSnapshotInfoValue.Set(
snapshotID,
tx,
walRecorder,
allocator,
Expand All @@ -428,24 +421,23 @@ func (db *DB) deleteSnapshot(
if err != nil {
return err
}
exists, err := previousSnapshotInfoValue.Exists(snapshotID,
tx, walRecorder, allocator, snapshotHashBuff, snapshotHashMatches)
exists, err := previousSnapshotInfoValue.Exists(tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}
if !exists {
return errors.Errorf("previous snapshot %d does not exist", snapshotID)
}

previousSnapshotInfo, err := previousSnapshotInfoValue.Value(snapshotID, tx, walRecorder, allocator,
snapshotHashBuff, snapshotHashMatches)
previousSnapshotInfo, err := previousSnapshotInfoValue.Value(tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}
previousSnapshotInfo.NextSnapshotID = snapshotInfo.NextSnapshotID

if err := previousSnapshotInfoValue.Set(
snapshotID,
tx,
walRecorder,
allocator,
Expand Down Expand Up @@ -486,14 +478,14 @@ func (db *DB) commit(
if err != nil {
return err
}
exists, err := deallocationListValue.Exists(commitSnapshotID, tx, walRecorder, allocator,
deallocationHashBuff, deallocationHashMatches)
exists, err := deallocationListValue.Exists(tx, walRecorder, allocator, deallocationHashBuff,
deallocationHashMatches)
if err != nil {
return err
}
if exists {
v, err := deallocationListValue.Value(commitSnapshotID, tx, walRecorder, allocator,
deallocationHashBuff, deallocationHashMatches)
v, err := deallocationListValue.Value(tx, walRecorder, allocator, deallocationHashBuff,
deallocationHashMatches)
if err != nil {
return err
}
Expand All @@ -503,7 +495,6 @@ func (db *DB) commit(
}
}
if err := deallocationListValue.Set(
commitSnapshotID,
tx,
walRecorder,
allocator,
Expand All @@ -525,7 +516,6 @@ func (db *DB) commit(
return err
}
if err := nextSnapshotInfoValue.Set(
commitSnapshotID,
tx,
walRecorder,
allocator,
Expand Down Expand Up @@ -610,8 +600,7 @@ func (db *DB) executeTransactions(ctx context.Context, pipeReader *pipeline.Read
if req.Transaction != nil {
switch tx := req.Transaction.(type) {
case *transfer.Tx:
if err := tx.Execute(db.singularityNode.LastSnapshotID, req, walRecorder, allocator, hashBuff,
hashMatches); err != nil {
if err := tx.Execute(req, walRecorder, allocator, hashBuff, hashMatches); err != nil {
return err
}
case *commitTx:
Expand Down
Loading

0 comments on commit 57b96e7

Please sign in to comment.