Skip to content

Commit

Permalink
Move test function back, fixup metrics and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed May 2, 2024
1 parent 780de04 commit be5555d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/daemon/interfaces/noOpDaemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func MakeNoOpDeamon() *noOpDaemon {
}

func (d *noOpDaemon) MetricsRegistry() *prometheus.Registry {
return d.metricsRegistry
return prometheus.NewRegistry() // so that you can register metrics many times
}

func (d *noOpDaemon) MetricsNamespace() string {
Expand Down
22 changes: 0 additions & 22 deletions cmd/soroban-rpc/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"database/sql"
"embed"
"fmt"
"path"
"strconv"
"sync"
"testing"

sq "github.com/Masterminds/squirrel"
_ "github.com/mattn/go-sqlite3"
"github.com/prometheus/client_golang/prometheus"
migrate "github.com/rubenv/sql-migrate"
"github.com/stretchr/testify/assert"

"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
Expand Down Expand Up @@ -333,22 +330,3 @@ func runMigrations(db *sql.DB, dialect string) error {
_, err := migrate.ExecMax(db, dialect, m, migrate.Up, 0)
return err
}

// NewTestDB is here so that other packages in sRPC can use it
func NewTestDB(tb testing.TB) *DB {
tmp := tb.TempDir()
dbPath := path.Join(tmp, "db.sqlite")
db, err := OpenSQLiteDB(dbPath)
if err != nil {
assert.NoError(tb, db.Close())
}
tb.Cleanup(func() {
assert.NoError(tb, db.Close())
})
return &DB{
SessionInterface: db,
cache: dbCache{
ledgerEntries: newTransactionalCache(),
},
}
}
19 changes: 19 additions & 0 deletions cmd/soroban-rpc/internal/db/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -100,3 +101,21 @@ func TestLedgers(t *testing.T) {

assertLedgerRange(t, reader, 8, 12)
}

func NewTestDB(tb testing.TB) *DB {
tmp := tb.TempDir()
dbPath := path.Join(tmp, "db.sqlite")
db, err := OpenSQLiteDB(dbPath)
if err != nil {
assert.NoError(tb, db.Close())

Check failure on line 110 in cmd/soroban-rpc/internal/db/ledger_test.go

View workflow job for this annotation

GitHub Actions / golangci

db.Close undefined (type *DB has no field or method Close) (typecheck)
}
tb.Cleanup(func() {
assert.NoError(tb, db.Close())

Check failure on line 113 in cmd/soroban-rpc/internal/db/ledger_test.go

View workflow job for this annotation

GitHub Actions / golangci

db.Close undefined (type *DB has no field or method Close) (typecheck)
})
return &DB{
SessionInterface: db,
cache: dbCache{
ledgerEntries: newTransactionalCache(),
},
}
}
10 changes: 7 additions & 3 deletions cmd/soroban-rpc/internal/preflight/preflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/require"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db"
)

Expand Down Expand Up @@ -294,15 +295,18 @@ func getDB(t testing.TB, restartDB bool) *db.DB {
dbPath := path.Join(t.TempDir(), "soroban_rpc.sqlite")
dbInstance, err := db.OpenSQLiteDB(dbPath)
require.NoError(t, err)
readWriter := db.NewReadWriter(log.DefaultLogger, dbInstance, 100, 10000, network.FutureNetworkPassphrase)

readWriter := db.NewReadWriter(log.DefaultLogger, dbInstance, interfaces.MakeNoOpDeamon(),
100, 10000, network.FutureNetworkPassphrase)
tx, err := readWriter.NewTx(context.Background())
require.NoError(t, err)

for _, e := range mockLedgerEntries {
err := tx.LedgerEntryWriter().UpsertLedgerEntry(e)
require.NoError(t, err)
}
err = tx.Commit(2)
require.NoError(t, err)
require.NoError(t, tx.Commit(2))

if restartDB {
// Restarting the DB resets the ledger entries write-through cache
require.NoError(t, dbInstance.Close())
Expand Down

0 comments on commit be5555d

Please sign in to comment.