|
| 1 | +// Copyright 2024 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package migration_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/ethersphere/bee/v2/pkg/sharky" |
| 13 | + "github.com/ethersphere/bee/v2/pkg/storage/inmemstore" |
| 14 | + "github.com/ethersphere/bee/v2/pkg/storer/internal/reserve" |
| 15 | + "github.com/ethersphere/bee/v2/pkg/storer/internal/transaction" |
| 16 | + localmigration "github.com/ethersphere/bee/v2/pkg/storer/migration" |
| 17 | + "github.com/ethersphere/bee/v2/pkg/swarm" |
| 18 | + "github.com/ethersphere/bee/v2/pkg/util/testutil" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +func Test_ResetEpochTimestamp(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + sharkyDir := t.TempDir() |
| 27 | + sharkyStore, err := sharky.New(&dirFS{basedir: sharkyDir}, 1, swarm.SocMaxChunkSize) |
| 28 | + assert.NoError(t, err) |
| 29 | + store := inmemstore.New() |
| 30 | + storage := transaction.NewStorage(sharkyStore, store) |
| 31 | + testutil.CleanupCloser(t, storage) |
| 32 | + |
| 33 | + err = storage.Run(context.Background(), func(s transaction.Store) error { |
| 34 | + return s.IndexStore().Put(&reserve.EpochItem{Timestamp: uint64(time.Now().Second())}) |
| 35 | + }) |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + has, err := storage.IndexStore().Has(&reserve.EpochItem{}) |
| 39 | + require.NoError(t, err) |
| 40 | + if !has { |
| 41 | + t.Fatal("epoch item should exist") |
| 42 | + } |
| 43 | + |
| 44 | + err = localmigration.ResetEpochTimestamp(storage)() |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + has, err = storage.IndexStore().Has(&reserve.EpochItem{}) |
| 48 | + require.NoError(t, err) |
| 49 | + if has { |
| 50 | + t.Fatal("epoch item should be deleted") |
| 51 | + } |
| 52 | +} |
0 commit comments