This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: - added first focused test that integrates DataSources, NounDataSource, and marshaled.DataSource - added name field to update data - simplified DataSources.Add method name - implemented data source removal with observation - made formatNames order stable under marshaled.NewDataSource - addressed some govet errors, and started a makefile to ease running tests and linting Reviewers: abg Reviewed By: abg
- Loading branch information
Joshua T Corbin
committed
May 19, 2016
1 parent
6e2ac25
commit 26ecc9d
Showing
12 changed files
with
175 additions
and
23 deletions.
There are no files selected for viewing
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,7 @@ | ||
.PHONY: lint | ||
|
||
lint: | ||
go vet $$(glide novendor) | ||
|
||
test: lint | ||
go test $$(glide novendor) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package meta_test | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/uber-go/gwr/internal/marshaled" | ||
"github.com/uber-go/gwr/internal/meta" | ||
"github.com/uber-go/gwr/source" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type dummyDataSource struct { | ||
name string | ||
attrs map[string]interface{} | ||
tmpl *template.Template | ||
} | ||
|
||
func (dds *dummyDataSource) Name() string { | ||
return dds.name | ||
} | ||
|
||
func (dds *dummyDataSource) Attrs() map[string]interface{} { | ||
return dds.attrs | ||
} | ||
|
||
func (dds *dummyDataSource) TextTemplate() *template.Template { | ||
return dds.tmpl | ||
} | ||
|
||
func (dds *dummyDataSource) Get() interface{} { | ||
return nil | ||
} | ||
|
||
func (dds *dummyDataSource) GetInit() interface{} { | ||
return nil | ||
} | ||
|
||
func (dds *dummyDataSource) SetWatcher(watcher source.GenericDataWatcher) { | ||
} | ||
|
||
func setup() *source.DataSources { | ||
dss := source.NewDataSources() | ||
nds := meta.NewNounDataSource(dss) | ||
dss.Add(marshaled.NewDataSource(nds, nil)) | ||
dss.SetObserver(nds) | ||
return dss | ||
} | ||
|
||
func TestNounDataSource_Watch(t *testing.T) { | ||
dss := setup() | ||
mds := dss.Get("/meta/nouns") | ||
|
||
r, w, err := os.Pipe() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// buf := newInternalRWC() | ||
sc := bufio.NewScanner(r) | ||
if err := mds.Watch("json", w); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// verify init data | ||
assertJSONScanLine(t, sc, | ||
`{"/meta/nouns":{"formats":["json","text"],"attrs":null}}`, | ||
"should get /meta/nouns initially") | ||
|
||
// add a data source, observe it | ||
assert.NoError(t, dss.Add(marshaled.NewDataSource(&dummyDataSource{ | ||
name: "/foo", | ||
attrs: map[string]interface{}{"aKey": "aVal"}, | ||
tmpl: nil, | ||
}, nil)), "no add error expected") | ||
assertJSONScanLine(t, sc, | ||
`{"name":"/foo","type":"add","info":{"formats":["json"],"attrs":{"aKey":"aVal"}}}`, | ||
"should get an add event for /foo") | ||
|
||
// add another data source, observe it | ||
assert.NoError(t, dss.Add(marshaled.NewDataSource(&dummyDataSource{ | ||
name: "/bar", | ||
attrs: map[string]interface{}{"bKey": 123}, | ||
tmpl: template.Must(template.New("bar_tmpl").Parse("")), | ||
}, nil)), "no add error expected") | ||
assertJSONScanLine(t, sc, | ||
`{"name":"/bar","type":"add","info":{"formats":["json","text"],"attrs":{"bKey":123}}}`, | ||
"should get an add event for /bar") | ||
|
||
// remove the /foo data source, observe it | ||
assert.NotNil(t, dss.Remove("/foo"), "expected a removed data source") | ||
assertJSONScanLine(t, sc, | ||
`{"name":"/foo","type":"remove"}`, | ||
"should get a remove event for /foo") | ||
|
||
// remove the /bar data source, observe it | ||
assert.NotNil(t, dss.Remove("/bar"), "expected a removed data source") | ||
assertJSONScanLine(t, sc, | ||
`{"name":"/bar","type":"remove"}`, | ||
"should get a remove event for /bar") | ||
|
||
// shutdown the watch stream | ||
assert.NoError(t, r.Close()) | ||
assert.False(t, sc.Scan(), "no more scan") | ||
} | ||
|
||
func assertJSONScanLine(t *testing.T, sc *bufio.Scanner, expected string, msgAndArgs ...interface{}) { | ||
if !sc.Scan() { | ||
assert.Fail(t, "expected to scan a JSON line", msgAndArgs...) | ||
} else { | ||
expected = strings.Join([]string{expected, "\n"}, "") | ||
assert.JSONEq(t, sc.Text(), expected, msgAndArgs...) | ||
} | ||
assert.NoError(t, sc.Err()) | ||
} |
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
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
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