Skip to content

Commit

Permalink
Test delayed deallocation (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Dec 22, 2024
1 parent 0de21b3 commit b068a1b
Showing 1 changed file with 113 additions and 6 deletions.
119 changes: 113 additions & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/samber/lo"
"github.com/stretchr/testify/require"

"github.com/outofforest/quantum/list"
"github.com/outofforest/quantum/pipeline"
"github.com/outofforest/quantum/space"
"github.com/outofforest/quantum/state"
Expand All @@ -27,19 +28,56 @@ func TestImmediateDeallocation(t *testing.T) {
persistentAddress, err := persistentAllocator.Allocate()
requireT.NoError(err)

for {
if _, err := persistentAllocator.Allocate(); err != nil {
break
}
}

listRoot, err := deallocateNode(appState, 10, 1, persistentAddress,
deallocationListsToCommit, volatileAllocator, persistentAllocator, persistentDeallocator, true)
requireT.NoError(err)
requireT.Zero(listRoot.VolatileAddress)
requireT.Zero(listRoot.PersistentAddress)
requireT.Empty(deallocationListsToCommit)

persistentDeallocator.Deallocate(0x00)
appState.Commit()

_, err = persistentAllocator.Allocate()
requireT.NoError(err)

deallocatedPersistentAddress, err := persistentAllocator.Allocate()
requireT.NoError(err)

requireT.Equal(persistentAddress, deallocatedPersistentAddress)
}

func TestSameSnapshotDeallocation(t *testing.T) {
requireT := require.New(t)

appState := state.NewForTest(t, 10*types.NodeLength)
volatileAllocator := appState.NewVolatileAllocator()
persistentAllocator := appState.NewPersistentAllocator()
persistentDeallocator := appState.NewPersistentDeallocator()
deallocationListsToCommit := map[types.SnapshotID]*types.ListRoot{}

persistentAddress, err := persistentAllocator.Allocate()
requireT.NoError(err)

for {
if _, err := persistentAllocator.Allocate(); err != nil {
break
}
}

listRoot, err := deallocateNode(appState, 10, 10, persistentAddress,
deallocationListsToCommit, volatileAllocator, persistentAllocator, persistentDeallocator, false)
requireT.NoError(err)
requireT.Zero(listRoot.VolatileAddress)
requireT.Zero(listRoot.PersistentAddress)
requireT.Empty(deallocationListsToCommit)

persistentDeallocator.Deallocate(0x00)
appState.Commit()

Expand All @@ -52,24 +90,57 @@ func TestImmediateDeallocation(t *testing.T) {
requireT.Equal(persistentAddress, deallocatedPersistentAddress)
}

func TestSameSnapshotDeallocation(t *testing.T) {
func TestDelayedDeallocation(t *testing.T) {
requireT := require.New(t)

const (
nodeSnapshotID1 = 1
nodeSnapshotID2 = 2
)

appState := state.NewForTest(t, 10*types.NodeLength)
volatileAllocator := appState.NewVolatileAllocator()
volatileDeallocator := appState.NewVolatileDeallocator()
persistentAllocator := appState.NewPersistentAllocator()
persistentDeallocator := appState.NewPersistentDeallocator()
deallocationListsToCommit := map[types.SnapshotID]*types.ListRoot{}

persistentAddress, err := persistentAllocator.Allocate()
persistentAddress1, err := persistentAllocator.Allocate()
requireT.NoError(err)
persistentAddress2, err := persistentAllocator.Allocate()
requireT.NoError(err)
persistentAddress3, err := persistentAllocator.Allocate()
requireT.NoError(err)

listRoot, err := deallocateNode(appState, 10, 10, persistentAddress,
listRoot, err := deallocateNode(appState, 10, nodeSnapshotID1, persistentAddress1,
deallocationListsToCommit, volatileAllocator, persistentAllocator, persistentDeallocator, false)
requireT.NoError(err)
requireT.Zero(listRoot.VolatileAddress)
requireT.Zero(listRoot.PersistentAddress)
requireT.Empty(deallocationListsToCommit)
requireT.Len(deallocationListsToCommit, 1)
requireT.NotNil(deallocationListsToCommit[nodeSnapshotID1])
requireT.NotZero(deallocationListsToCommit[nodeSnapshotID1].VolatileAddress)
requireT.NotZero(deallocationListsToCommit[nodeSnapshotID1].PersistentAddress)

previousListRoot := *deallocationListsToCommit[nodeSnapshotID1]

listRoot, err = deallocateNode(appState, 10, nodeSnapshotID1, persistentAddress2,
deallocationListsToCommit, volatileAllocator, persistentAllocator, persistentDeallocator, false)
requireT.NoError(err)
requireT.Zero(listRoot.VolatileAddress)
requireT.Zero(listRoot.PersistentAddress)
requireT.Len(deallocationListsToCommit, 1)
requireT.NotNil(deallocationListsToCommit[nodeSnapshotID1])
requireT.Equal(previousListRoot, *deallocationListsToCommit[nodeSnapshotID1])

listRoot, err = deallocateNode(appState, 10, nodeSnapshotID2, persistentAddress3,
deallocationListsToCommit, volatileAllocator, persistentAllocator, persistentDeallocator, false)
requireT.NoError(err)
requireT.Zero(listRoot.VolatileAddress)
requireT.Zero(listRoot.PersistentAddress)
requireT.Len(deallocationListsToCommit, 2)
requireT.NotNil(deallocationListsToCommit[nodeSnapshotID2])
requireT.NotEqual(previousListRoot, *deallocationListsToCommit[nodeSnapshotID2])

for {
if _, err := persistentAllocator.Allocate(); err != nil {
Expand All @@ -83,10 +154,46 @@ func TestSameSnapshotDeallocation(t *testing.T) {
_, err = persistentAllocator.Allocate()
requireT.NoError(err)

deallocatedPersistentAddress, err := persistentAllocator.Allocate()
_, err = persistentAllocator.Allocate()
requireT.Error(err)

requireT.NoError(list.Deallocate(*deallocationListsToCommit[nodeSnapshotID1], appState, volatileDeallocator,
persistentDeallocator))

persistentDeallocator.Deallocate(0x00)
appState.Commit()

_, err = persistentAllocator.Allocate()
requireT.NoError(err)

requireT.Equal(persistentAddress, deallocatedPersistentAddress)
listRootPersistentAddress, err := persistentAllocator.Allocate()
requireT.NoError(err)
requireT.Equal(listRootPersistentAddress, deallocationListsToCommit[nodeSnapshotID1].PersistentAddress)

deallocatedPersistentAddress1, err := persistentAllocator.Allocate()
requireT.NoError(err)
requireT.Equal(persistentAddress1, deallocatedPersistentAddress1)

deallocatedPersistentAddress2, err := persistentAllocator.Allocate()
requireT.NoError(err)
requireT.Equal(persistentAddress2, deallocatedPersistentAddress2)

requireT.NoError(list.Deallocate(*deallocationListsToCommit[nodeSnapshotID2], appState, volatileDeallocator,
persistentDeallocator))

persistentDeallocator.Deallocate(0x00)
appState.Commit()

_, err = persistentAllocator.Allocate()
requireT.NoError(err)

listRootPersistentAddress, err = persistentAllocator.Allocate()
requireT.NoError(err)
requireT.Equal(listRootPersistentAddress, deallocationListsToCommit[nodeSnapshotID2].PersistentAddress)

deallocatedPersistentAddress3, err := persistentAllocator.Allocate()
requireT.NoError(err)
requireT.Equal(persistentAddress3, deallocatedPersistentAddress3)
}

func TestPipe01PrepareTransactionsDoesNothingIfTransactionIsNil(t *testing.T) {
Expand Down

0 comments on commit b068a1b

Please sign in to comment.