Skip to content

Commit

Permalink
Merge 26edb84 into 5668d21
Browse files Browse the repository at this point in the history
  • Loading branch information
ns11t committed Jun 24, 2020
2 parents 5668d21 + 26edb84 commit 52267db
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
18 changes: 16 additions & 2 deletions builder.go
@@ -1,13 +1,23 @@
package stl

import "context"
import (
"context"

"github.com/gofrs/uuid"
)

// New creates an instance of default Builder.
func New() Builder {
return &builder{}
id, err := uuid.NewV4()
if err != nil {
panic(err)
}

return &builder{id: id.String()}
}

type builder struct {
id string
shs []string
exs []string
v Vault
Expand All @@ -33,6 +43,10 @@ func (t *builder) Exclusive(name string) Builder {
return t
}

func (t *builder) ID() string {
return t.id
}

func (t *builder) ListShared() []string {
return t.shs
}
Expand Down
10 changes: 9 additions & 1 deletion merge_tx.go
@@ -1,5 +1,7 @@
package stl

import "github.com/gofrs/uuid"

// MergeTx prepares a single transaction from several simultaneous ones.
func MergeTx(ttx ...Tx) Tx {
txMap := map[string]bool{}
Expand All @@ -23,9 +25,15 @@ func MergeTx(ttx ...Tx) Tx {
}
}

id, err := uuid.NewV4()
if err != nil {
panic(err)
}

return &builder{
id: id.String(),
shs: shs,
exs: exs,
v: nil,
}
}
}
18 changes: 16 additions & 2 deletions stacked_builder.go
@@ -1,13 +1,23 @@
package stl

import "context"
import (
"context"

"github.com/gofrs/uuid"
)

// NewStacked creates an instance of stacked Builder.
func NewStacked() Builder {
return &stackedBuilder{}
id, err := uuid.NewV4()
if err != nil {
panic(err)
}

return &stackedBuilder{id: id.String()}
}

type stackedBuilder struct {
id string
prefix string
shs []string
exs []string
Expand Down Expand Up @@ -44,6 +54,10 @@ func (t *stackedBuilder) ListExclusive() []string {
return t.exs
}

func (t *stackedBuilder) ID() string {
return t.id
}

func (t *stackedBuilder) ToTx() Tx {
return t
}
Expand Down
1 change: 1 addition & 0 deletions stl.go
Expand Up @@ -15,6 +15,7 @@ type Locker interface {
// Tx holds the names of shared and exclusive resources to be locked
// atomically.
type Tx interface {
ID() string
ListShared() []string
ListExclusive() []string
}
Expand Down
6 changes: 6 additions & 0 deletions stl_test.go
Expand Up @@ -348,6 +348,11 @@ func TestDiningPhilosophers(t *testing.T) {
wg.Wait()
}

func TestGenerateID(t *testing.T) {
require.NotEmpty(t, New().ToTx().ID())
require.NotEmpty(t, NewStacked().ToTx().ID())
}

func TestMergeTx(t *testing.T) {
ttx := []Tx{
NewStacked().Shared("resource1").Shared("resource2").Exclusive("resource3").ToTx(),
Expand All @@ -371,6 +376,7 @@ func TestMergeTx(t *testing.T) {

require.ElementsMatch(t, expectedExs, tx.ListExclusive())
require.ElementsMatch(t, expectedShs, tx.ListShared())
require.NotEmpty(t, tx.ID())
}

func TestTryLock(t *testing.T) {
Expand Down

0 comments on commit 52267db

Please sign in to comment.