This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
generated from yandex-praktikum/go-musthave-devops-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: Add unit-tests for file backed storage
Signed-off-by: Alexander Kurbatov <sir.alkurbatov@yandex.ru>
- Loading branch information
1 parent
446840b
commit 7976a9f
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package storage_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/alkurbatov/metrics-collector/internal/storage" | ||
"github.com/alkurbatov/metrics-collector/pkg/metrics" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createStoreWithData( | ||
t *testing.T, | ||
storePath string, | ||
syncMode bool, | ||
) *storage.FileBackedStorage { | ||
t.Helper() | ||
|
||
ctx := context.Background() | ||
store := storage.NewFileBackedStorage(storePath, syncMode) | ||
|
||
batch := map[string]storage.Record{ | ||
"PollCount_counter": {Name: "PollCount", Value: metrics.Counter(10)}, | ||
"Alloc_gauge": {Name: "Alloc", Value: metrics.Gauge(11.356)}, | ||
} | ||
err := store.PushBatch(ctx, batch) | ||
require.NoError(t, err) | ||
|
||
err = store.Push( | ||
ctx, | ||
"HeapSys_gauge", | ||
storage.Record{Name: "HeapSys", Value: metrics.Gauge(7831552)}, | ||
) | ||
require.NoError(t, err) | ||
|
||
err = store.Push( | ||
ctx, | ||
"RequestsCount_counter", | ||
storage.Record{Name: "RequestsCount", Value: metrics.Counter(1)}, | ||
) | ||
require.NoError(t, err) | ||
|
||
return store | ||
} | ||
|
||
func TestSyncDumpRestoreStorage(t *testing.T) { | ||
storePath := "/tmp/test-sync-dump-restore.json" | ||
|
||
t.Cleanup(func() { | ||
err := os.Remove(storePath) | ||
require.NoError(t, err) | ||
}) | ||
|
||
store := createStoreWithData(t, storePath, true) | ||
storedData := store.Snapshot() | ||
|
||
store = storage.NewFileBackedStorage(storePath, true) | ||
err := store.Restore() | ||
require.NoError(t, err) | ||
|
||
restoredData := store.Snapshot() | ||
require.Equal(t, storedData, restoredData) | ||
} | ||
|
||
func TestAsyncDumpRestoreStorage(t *testing.T) { | ||
storePath := "/tmp/test-async-dump-restore.json" | ||
|
||
t.Cleanup(func() { | ||
err := os.Remove(storePath) | ||
require.NoError(t, err) | ||
}) | ||
|
||
store := createStoreWithData(t, storePath, false) | ||
storedData := store.Snapshot() | ||
|
||
err := store.Close(context.Background()) | ||
require.NoError(t, err) | ||
|
||
store = storage.NewFileBackedStorage(storePath, false) | ||
err = store.Restore() | ||
require.NoError(t, err) | ||
|
||
restoredData := store.Snapshot() | ||
require.Equal(t, storedData, restoredData) | ||
} | ||
|
||
func TestRestoreDoesntFailIfNoSourceFile(t *testing.T) { | ||
store := storage.NewFileBackedStorage("xxx", false) | ||
|
||
err := store.Restore() | ||
require.NoError(t, err) | ||
} |