-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04d7011
commit 910ffec
Showing
9 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package adapter | ||
|
||
import ( | ||
"fmt" | ||
|
||
"Growth/core/entity" | ||
) | ||
|
||
type ExperimentStore interface { | ||
Save(entity.Experiment) entity.Experiment | ||
FetchByID(id entity.ID) entity.Experiment | ||
ErrNotFound() *ErrExperimentNotFound | ||
ErrOther() error | ||
} | ||
|
||
type ErrExperimentNotFound struct { | ||
ID entity.ID | ||
Stack string | ||
} | ||
|
||
func (e *ErrExperimentNotFound) Error() string { | ||
return fmt.Sprintf("experiment:%d not found", e.ID) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package testadapter | ||
|
||
import ( | ||
"runtime/debug" | ||
|
||
"Growth/core/adapter" | ||
"Growth/core/entity" | ||
) | ||
|
||
type FakeExperimentStore struct { | ||
experiments []entity.Experiment | ||
errNotFound *adapter.ErrExperimentNotFound | ||
errOther error | ||
} | ||
|
||
func (store *FakeExperimentStore) Save(e entity.Experiment) entity.Experiment { | ||
store.init() | ||
e.ID = entity.ID(len(store.experiments) + 1) | ||
store.experiments = append(store.experiments, e) | ||
return e | ||
} | ||
|
||
func (store *FakeExperimentStore) FetchByID(id entity.ID) entity.Experiment { | ||
for _, e := range store.experiments { | ||
if e.ID == id { | ||
return e | ||
} | ||
} | ||
store.errNotFound =&adapter.ErrExperimentNotFound{ID: id, Stack: string(debug.Stack())} | ||
return entity.Experiment{} | ||
} | ||
|
||
func (store *FakeExperimentStore) ErrNotFound() *adapter.ErrExperimentNotFound { | ||
defer func() { | ||
store.errNotFound = nil | ||
}() | ||
return store.errNotFound | ||
} | ||
|
||
func (store *FakeExperimentStore) ErrNotFoundTrace() *adapter.ErrExperimentNotFound { | ||
defer func() { | ||
store.errNotFound = nil | ||
}() | ||
return store.errNotFound | ||
} | ||
|
||
func (store *FakeExperimentStore) ErrOther() error { | ||
defer func() { | ||
store.errOther = nil | ||
}() | ||
return store.errOther | ||
} | ||
|
||
func (store *FakeExperimentStore) init() { | ||
if store == nil { | ||
*store = FakeExperimentStore{} | ||
} | ||
if store.experiments == nil { | ||
store.experiments = make([]entity.Experiment, 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package usecase | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type EventType int | ||
|
||
type Event struct { | ||
Type EventType | ||
Data interface{} | ||
} | ||
|
||
type EventBus struct { | ||
qs map[EventType][]chan Out | ||
} | ||
|
||
func (bus *EventBus) init() { | ||
if bus.qs == nil { | ||
bus.qs = make(map[EventType][]chan Out) | ||
} | ||
} | ||
|
||
func (bus *EventBus) Emit(e Event) error { | ||
bus.init() | ||
for _, out := range bus.qs[e.Type] { | ||
go func(out chan Out) { | ||
out <- Out{ | ||
Data: e.Data, | ||
Error: nil, | ||
} | ||
}(out) | ||
} | ||
return nil | ||
} | ||
|
||
type Out struct { | ||
Data interface{} | ||
Error error | ||
} | ||
|
||
func (bus *EventBus) On(et EventType) chan Out { | ||
bus.init() | ||
// register a queue for this listener | ||
outs := bus.qs[et] | ||
out := make(chan Out) | ||
bus.qs[et] = append(outs, out) | ||
return out | ||
} | ||
|
||
const ( | ||
EventX EventType = iota | ||
EventY | ||
EventZ | ||
) | ||
|
||
func TestX(t *testing.T) { | ||
defer func() { | ||
recover() | ||
}() | ||
|
||
bus := &EventBus{} | ||
|
||
xChan := bus.On(EventX) | ||
xChan2 := bus.On(EventX) | ||
|
||
bus.Emit(Event{ | ||
Type: EventType(EventX), | ||
Data: "1", | ||
}) | ||
bus.Emit(Event{ | ||
Type: EventType(EventX), | ||
Data: "1", | ||
}) | ||
|
||
out := <-xChan | ||
require.Equal(t, "1", out.Data) | ||
require.Equal(t, nil, out.Error) | ||
|
||
out = <-xChan2 | ||
require.Equal(t, "1", out.Data) | ||
require.Equal(t, nil, out.Error) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
package usecase | ||
|
||
import "Growth/core/entity" | ||
import ( | ||
"Growth/core/adapter" | ||
"Growth/core/entity" | ||
) | ||
|
||
func CreateExperiment(userID entity.ID) (entity.Experiment, error) { | ||
return entity.Experiment{Owner: userID}, nil | ||
func NewExperiment(store adapter.ExperimentStore) *Experiment { | ||
return &Experiment{store: store} | ||
} | ||
|
||
type Experiment struct { | ||
store adapter.ExperimentStore | ||
errExperimentNotFound *adapter.ErrExperimentNotFound | ||
errOther error | ||
} | ||
|
||
func (e *Experiment) CreateExperiment(userID entity.ID) (entity.Experiment) { | ||
exp := e.store.Save(entity.Experiment{Owner: userID}) | ||
e.errOther = e.store.ErrOther() | ||
return exp | ||
} | ||
|
||
func (e *Experiment) FetchExperimentByID(id entity.ID) entity.Experiment { | ||
exp := e.store.FetchByID(id) | ||
e.errExperimentNotFound = e.store.ErrNotFound() | ||
e.errOther = e.store.ErrOther() | ||
return exp | ||
} | ||
|
||
func (e *Experiment) ErrExperimentNotFound() *adapter.ErrExperimentNotFound { | ||
defer func() { | ||
e.errExperimentNotFound = nil | ||
}() | ||
return e.errExperimentNotFound | ||
} | ||
|
||
func (e *Experiment) ErrOther() error { | ||
defer func() { | ||
e.errOther = nil | ||
}() | ||
return e.errOther | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters