Connect sign-up to Sendico Facts intake (#46)
This commit is contained in:
1 parent
30f0455122
commit
a584c8189a
40 files changed
+1616
-35
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
|
||||
)
|
||||
|
||||
type majority struct{ Source }
|
||||
|
||||
// Majority retains the source's lease and transaction lifecycle while requiring
|
||||
// durable writes and primary majority reads for externally visible handoffs.
|
||||
func Majority(database Source) Source { return &majority{Source: database} }
|
||||
|
||||
func (s *majority) Acquire(ctx context.Context) (*mongo.Database, context.Context, func(), error) {
|
||||
database, scoped, release, err := s.Source.Acquire(ctx)
|
||||
if err != nil {
|
||||
return nil, scoped, release, err
|
||||
}
|
||||
return database.Client().Database(database.Name(), options.Database().SetWriteConcern(writeconcern.Majority()).SetReadConcern(readconcern.Majority()).SetReadPreference(readpref.Primary())), scoped, release, nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
|
||||
)
|
||||
|
||||
type trackedSource struct {
|
||||
database *mongo.Database
|
||||
released bool
|
||||
}
|
||||
|
||||
func (s *trackedSource) Acquire(ctx context.Context) (*mongo.Database, context.Context, func(), error) {
|
||||
return s.database, ctx, func() { s.released = true }, nil
|
||||
}
|
||||
|
||||
func TestMajorityPreservesSourceLifetime(t *testing.T) {
|
||||
client, err := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017").SetWriteConcern(&writeconcern.WriteConcern{W: 1}).SetReadPreference(readpref.Secondary()))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { require.NoError(t, client.Disconnect(context.Background())) })
|
||||
underlying := &trackedSource{database: client.Database("handoff")}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
database, scoped, release, err := Majority(underlying).Acquire(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Same(t, client, database.Client())
|
||||
require.Equal(t, "handoff", database.Name())
|
||||
require.Equal(t, ctx, scoped)
|
||||
require.False(t, underlying.released)
|
||||
release()
|
||||
require.True(t, underlying.released)
|
||||
_, _, _, err = Majority(Fixed(nil)).Acquire(ctx)
|
||||
require.Error(t, err)
|
||||
}
|
||||
Reference in new issue
Block a user