Skip to content

Commit

Permalink
remove some redudancy and add query options
Browse files Browse the repository at this point in the history
Change fake deps to store saved QueryOptions to enables testing their
being set properly.
  • Loading branch information
eikenb committed Nov 11, 2021
1 parent 09fb59f commit 2e0d7da
Showing 1 changed file with 21 additions and 49 deletions.
70 changes: 21 additions & 49 deletions internal/dependency/fakedep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type FakeDep struct {
isConsul
Name string
Opts QueryOptions
}

func (d *FakeDep) Fetch(dep.Clients) (interface{}, *dep.ResponseMetadata, error) {
Expand All @@ -23,24 +24,29 @@ func (d *FakeDep) Fetch(dep.Clients) (interface{}, *dep.ResponseMetadata, error)
return data, rm, nil
}

func (d *FakeDep) CanShare() bool {
return true
}

func (d *FakeDep) ID() string {
return fmt.Sprintf("test_dep(%s)", d.Name)
}
func (d *FakeDep) String() string {
return d.ID()
}

func (d *FakeDep) Stop() {}
func (d *FakeDep) SetOptions(opts QueryOptions) {}
func (d *FakeDep) CanShare() bool {
return true
}
func (d *FakeDep) Stop() {}
func (d *FakeDep) SetOptions(opts QueryOptions) {
d.Opts = opts
}
func (d *FakeDep) GetOptions() QueryOptions {
return d.Opts
}

////////////
// FakeListDep is a fake dependency that does not actually speaks to a server.
// Returns a list, to allow for multi-pass template tests
type FakeListDep struct {
FakeDep
Name string
Data []string
}
Expand All @@ -52,24 +58,18 @@ func (d *FakeListDep) Fetch(dep.Clients) (interface{}, *dep.ResponseMetadata, er
return data, rm, nil
}

func (d *FakeListDep) CanShare() bool {
return true
}

func (d *FakeListDep) ID() string {
return fmt.Sprintf("test_list_dep(%s)", d.Name)
}
func (d *FakeListDep) String() string {
return d.ID()
}

func (d *FakeListDep) Stop() {}
func (d *FakeListDep) SetOptions(opts QueryOptions) {}

////////////
// FakeDepStale is a fake dependency that can be used to test what happens
// when stale data is permitted.
type FakeDepStale struct {
FakeDep
Name string
}

Expand All @@ -81,7 +81,8 @@ func (d *FakeDepStale) Fetch(clients dep.Clients) (interface{}, *dep.ResponseMet

if opts.AllowStale {
data := "this is some stale data"
rm := &dep.ResponseMetadata{LastIndex: 1, LastContact: 50 * time.Millisecond}
rm := &dep.ResponseMetadata{
LastIndex: 1, LastContact: 50 * time.Millisecond}
return data, rm, nil
}

Expand All @@ -90,23 +91,17 @@ func (d *FakeDepStale) Fetch(clients dep.Clients) (interface{}, *dep.ResponseMet
return data, rm, nil
}

func (d *FakeDepStale) CanShare() bool {
return true
}

func (d *FakeDepStale) ID() string {
return fmt.Sprintf("test_dep_stale(%s)", d.Name)
}
func (d *FakeDepStale) String() string {
return d.ID()
}

func (d *FakeDepStale) Stop() {}
func (d *FakeDepStale) SetOptions(opts QueryOptions) {}

////////////
// FakeDepFetchError is a fake dependency that returns an error while fetching.
type FakeDepFetchError struct {
FakeDep
Name string
}

Expand All @@ -115,48 +110,37 @@ func (d *FakeDepFetchError) Fetch(dep.Clients) (interface{}, *dep.ResponseMetada
return nil, nil, fmt.Errorf("failed to contact server: connection refused")
}

func (d *FakeDepFetchError) CanShare() bool {
return true
}

func (d *FakeDepFetchError) ID() string {
return fmt.Sprintf("test_dep_fetch_error(%s)", d.Name)
}
func (d *FakeDepFetchError) String() string {
return d.ID()
}

func (d *FakeDepFetchError) Stop() {}
func (d *FakeDepFetchError) SetOptions(opts QueryOptions) {}

////////////
var _ isDependency = (*FakeDepSameIndex)(nil)

type FakeDepSameIndex struct{}
type FakeDepSameIndex struct {
FakeDep
}

func (d *FakeDepSameIndex) Fetch(dep.Clients) (interface{}, *dep.ResponseMetadata, error) {
meta := &dep.ResponseMetadata{LastIndex: 100}
return nil, meta, nil
}

func (d *FakeDepSameIndex) CanShare() bool {
return true
}

func (d *FakeDepSameIndex) ID() string {
return "test_dep_same_index"
}
func (d *FakeDepSameIndex) String() string {
return d.ID()
}

func (d *FakeDepSameIndex) Stop() {}
func (d *FakeDepSameIndex) SetOptions(opts QueryOptions) {}

////////////
// FakeDepRetry is a fake dependency that errors on the first fetch and
// succeeds on subsequent fetches.
type FakeDepRetry struct {
FakeDep
sync.Mutex
Name string
retried bool
Expand All @@ -178,23 +162,17 @@ func (d *FakeDepRetry) Fetch(dep.Clients) (interface{}, *dep.ResponseMetadata, e
return nil, nil, fmt.Errorf("failed to contact server (try again)")
}

func (d *FakeDepRetry) CanShare() bool {
return true
}

func (d *FakeDepRetry) ID() string {
return fmt.Sprintf("test_dep_retry(%s)", d.Name)
}
func (d *FakeDepRetry) String() string {
return d.ID()
}

func (d *FakeDepRetry) Stop() {}
func (d *FakeDepRetry) SetOptions(opts QueryOptions) {}

// FakeDepBlockingQuery is a fake dependency that blocks on Fetch for a
// duration to resemble Consul blocking queries.
type FakeDepBlockingQuery struct {
FakeDep
Name string
Data interface{}
BlockDuration time.Duration
Expand All @@ -217,10 +195,6 @@ func (d *FakeDepBlockingQuery) Fetch(dep.Clients) (interface{}, *dep.ResponseMet
}
}

func (d *FakeDepBlockingQuery) CanShare() bool {
return true
}

func (d *FakeDepBlockingQuery) ID() string {
return fmt.Sprintf("test_dep_blocking_query(%s)", d.Name)
}
Expand All @@ -233,5 +207,3 @@ func (d *FakeDepBlockingQuery) Stop() {
close(d.stop)
}
}

func (d *FakeDepBlockingQuery) SetOptions(opts QueryOptions) {}

0 comments on commit 2e0d7da

Please sign in to comment.