Skip to content

Commit

Permalink
Add logging, update filter
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Apr 7, 2022
1 parent 8b42c74 commit 87e5e23
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
3 changes: 3 additions & 0 deletions blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,14 @@ func (bl *Blocker) managedBlock() error {
ctx, cancel := context.WithTimeout(context.Background(), database.MongoDefaultTimeout)
defer cancel()

bl.staticLogger.Debugf("managedBlock blocking hashes from %v", from)

// Fetch hashes to block
hashes, err := bl.staticDB.HashesToBlock(ctx, from)
if err != nil {
return err
}
bl.staticLogger.Debugf("managedBlock found %d hashes", len(hashes))
if len(hashes) == 0 {
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ func (db *DB) BlockedHashes(ctx context.Context, sort, skip, limit int) ([]Block
opts.SetSort(bson.D{{"timestamp_added", sort}})

// fetch the documents
docs, err := db.find(ctx, bson.M{"invalid": bson.M{"$ne": true}}, opts)
docs, err := db.find(ctx, bson.M{
"invalid": bson.M{"$ne": true},
"hash": bson.M{"$exists": true},
}, opts)
if err != nil {
return nil, false, err
}
Expand Down
67 changes: 45 additions & 22 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func TestDatabase(t *testing.T) {
name string
test func(t *testing.T)
}{

{
name: "Ping",
test: testPing,
name: "BlockedHashes",
test: testBlockedHashes,
},

{
name: "CreateBlockedSkylink",
test: testCreateBlockedSkylink,
Expand Down Expand Up @@ -83,38 +85,30 @@ func TestDatabase(t *testing.T) {
name: "MarkInvalid",
test: testMarkInvalid,
},
{
name: "Ping",
test: testPing,
},
}
for _, test := range tests {
t.Run(test.name, test.test)
}
}

// testPing is a unit test for the database's Ping method.
func testPing(t *testing.T) {
// testBlockedHashes tests fetching blocked hashes from the database
func testBlockedHashes(t *testing.T) {
// create context
ctx, cancel := context.WithTimeout(context.Background(), MongoDefaultTimeout)
defer cancel()

// create test database
db := newTestDB(t.Name())

// ping should succeed
err := db.Ping(ctx)
if err != nil {
t.Fatal(err)
}

// close it
err = db.Close(ctx)
if err != nil {
t.Fatal(err)
}

// ping should fail
err = db.Ping(ctx)
if err == nil {
t.Fatal("should fail")
}
defer func() {
err := db.Close(ctx)
if err != nil {
t.Fatal(err)
}
}()
}

// testCreateBlockedSkylink tests creating and fetching a blocked skylink from
Expand Down Expand Up @@ -573,6 +567,35 @@ func testMarkInvalid(t *testing.T) {
}
}


// testPing is a unit test for the database's Ping method.
func testPing(t *testing.T) {
// create context
ctx, cancel := context.WithTimeout(context.Background(), MongoDefaultTimeout)
defer cancel()

// create test database
db := newTestDB(t.Name())

// ping should succeed
err := db.Ping(ctx)
if err != nil {
t.Fatal(err)
}

// close it
err = db.Close(ctx)
if err != nil {
t.Fatal(err)
}

// ping should fail
err = db.Ping(ctx)
if err == nil {
t.Fatal("should fail")
}
}

// define a helper function to decode a skylink as string into a skylink obj
func skylinkFromString(skylink string) (sl skymodules.Skylink) {
err := sl.LoadString(skylink)
Expand Down

0 comments on commit 87e5e23

Please sign in to comment.