func TestMergeFileSets(t *testing.T) { mergedSet := mergeFileSets( []File{{Name: "a"}, {Name: "b"}}, []File{{Name: "a"}, {Name: "c"}}) ensure.SameElements(t, []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}, mergedSet) mergedSet = mergeFileSets( []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}, []File{}) ensure.SameElements(t, []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}, mergedSet) }
func TestObjectString(t *testing.T) { var g inject.Graph a := &TypeNestedStruct{} if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil { t.Fatal(err) } var c TypeForObjectString if err := g.Provide(&inject.Object{Value: &c}); err != nil { t.Fatal(err) } if err := g.Populate(); err != nil { t.Fatal(err) } var actual []string for _, o := range g.Objects() { actual = append(actual, fmt.Sprint(o)) } ensure.SameElements(t, actual, []string{ "*inject_test.TypeForObjectString", "*inject_test.TypeNestedStruct", "*inject_test.TypeNestedStruct named foo", "*inject_test.TypeAnswerStruct", }) }
func TestPrefixClient(t *testing.T) { const ( prefix1 = "prefix1" prefix2 = "prefix2" avgKey = "avg" avgVal = float64(1) sumKey = "sum" sumVal = float64(2) histogramKey = "histogram" histogramVal = float64(3) timeKey = "time" ) var keys []string hc := &stats.HookClient{ BumpAvgHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, avgVal) }, BumpSumHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, sumVal) }, BumpHistogramHook: func(key string, val float64) { keys = append(keys, key) ensure.DeepEqual(t, val, histogramVal) }, BumpTimeHook: func(key string) interface { End() } { return multiEnderTest{ EndHook: func() { keys = append(keys, key) }, } }, } pc := stats.PrefixClient([]string{prefix1, prefix2}, hc) pc.BumpAvg(avgKey, avgVal) pc.BumpSum(sumKey, sumVal) pc.BumpHistogram(histogramKey, histogramVal) pc.BumpTime(timeKey).End() ensure.SameElements(t, keys, []string{ prefix1 + avgKey, prefix1 + sumKey, prefix1 + histogramKey, prefix1 + timeKey, prefix2 + avgKey, prefix2 + sumKey, prefix2 + histogramKey, prefix2 + timeKey, }) }
// HelpTestStorage runs basic tests on the given Storage func HelpTestStorage(t *testing.T, s Storage) { data1 := []byte("abcdef") data2 := strings.NewReader("xyz123") err := s.Put("a/data1", data1) ensure.Nil(t, err) err = s.PutReader("b/data2", data2) ensure.Nil(t, err) items, err := s.List("a", 1000) ensure.Nil(t, err) ensure.DeepEqual(t, len(items), 1) ensure.SameElements(t, items, []string{"a/data1"}) items, err = s.List("b/", 1000) ensure.Nil(t, err) ensure.DeepEqual(t, len(items), 1) ensure.SameElements(t, items, []string{"b/data2"}) data1Reader, err := s.Get("a/data1") ensure.Nil(t, err) data2Reader, err := s.Get("b/data2") ensure.Nil(t, err) content, err := ioutil.ReadAll(data1Reader) ensure.Nil(t, err) ensure.DeepEqual(t, content, []byte("abcdef")) content, err = ioutil.ReadAll(data2Reader) ensure.Nil(t, err) ensure.DeepEqual(t, content, []byte("xyz123")) err = s.Delete("a/data1") ensure.Nil(t, err) err = s.Delete("b/data2") ensure.Nil(t, err) items, err = s.List("", 1000) ensure.Nil(t, err) ensure.DeepEqual(t, len(items), 0) }
func TestSameElementsLengthDifference(t *testing.T) { var c capture ensure.SameElements(&c, []int{1, 2}, []interface{}{1}) c.Equal(t, `ensure_test.go:180: expected same elements but found slices of different lengths: ACTUAL: ([]int) (len=2 cap=2) { (int) 1, (int) 2 } EXPECTED ([]interface {}) (len=1 cap=1) { (int) 1 }`) }
func TestStatsTicker(t *testing.T) { t.Parallel() klock := clock.NewMock() expected := []string{"alive", "idle", "out", "waiting"} statsDone := make(chan string, 4) hc := &stats.HookClient{ BumpAvgHook: func(key string, val float64) { if contains(expected, key) { statsDone <- key } }, } var cm resourceMaker p := Pool{ New: cm.New, Stats: hc, Max: 4, MinIdle: 2, IdleTimeout: time.Second, ClosePoolSize: 2, Clock: klock, } // acquire and release some resources to make them idle var resources []io.Closer for i := p.Max; i > 0; i-- { r, err := p.Acquire() ensure.Nil(t, err) resources = append(resources, r) } for _, r := range resources { p.Release(r) } // tick IdleTimeout to make them eligible klock.Add(p.IdleTimeout) // tick Minute to trigger stats klock.Add(time.Minute) // stats should soon show idle closed ensure.SameElements( t, []string{<-statsDone, <-statsDone, <-statsDone, <-statsDone}, expected, ) }
func TestColumnFamilyCreateDrop(t *testing.T) { dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyCreate") ensure.Nil(t, err) opts := NewDefaultOptions() opts.SetCreateIfMissingColumnFamilies(true) opts.SetCreateIfMissing(true) db, err := OpenDb(opts, dir) ensure.Nil(t, err) defer db.Close() cf, err := db.CreateColumnFamily(opts, "guide") ensure.Nil(t, err) defer cf.Destroy() actualNames, err := ListColumnFamilies(opts, dir) ensure.Nil(t, err) ensure.SameElements(t, actualNames, []string{"default", "guide"}) ensure.Nil(t, db.DropColumnFamily(cf)) actualNames, err = ListColumnFamilies(opts, dir) ensure.Nil(t, err) ensure.SameElements(t, actualNames, []string{"default"}) }
func TestSameElementsRepeated(t *testing.T) { var c capture ensure.SameElements(&c, []int{1, 2}, []interface{}{1, 1}) c.Equal(t, `ensure_test.go:195: missing expected element: ACTUAL: ([]int) (len=2 cap=2) { (int) 1, (int) 2 } EXPECTED: ([]interface {}) (len=2 cap=2) { (int) 1, (int) 1 } MISSING ELEMENT (int) 1`) }
func TestMetadata(t *testing.T) { m := NewMetadataStore() md0 := SnapshotMetadata{ReplicaID: "replset1_host1", ID: "1", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}} md1 := SnapshotMetadata{ReplicaID: "replset1_host1", ID: "2", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}} md2 := SnapshotMetadata{ReplicaID: "replset2_host2", ID: "1", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}} err := m.Add(NewLazySMFromM(&md0)) ensure.Nil(t, err) err = m.Add(NewLazySMFromM(&md1)) ensure.Nil(t, err) err = m.Add(NewLazySMFromM(&md2)) ensure.Nil(t, err) ensure.SameElements(t, m.GetReplicaIDs(), []string{"replset1_host1", "replset2_host2"}) lazyMD, err := m.getForReplica("replset1_host1") ensure.Nil(t, err) ensure.DeepEqual(t, len(lazyMD), 2) mdRetrieved, err := lazyMD[0].Get() ensure.Nil(t, err) ensure.Subset(t, *mdRetrieved, SnapshotMetadata{ID: "1", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}}) mdRetrieved, err = lazyMD[1].Get() ensure.Nil(t, err) ensure.Subset(t, *mdRetrieved, SnapshotMetadata{ID: "2", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}}) lazyMD, err = m.getForReplica("replset2_host2") ensure.Nil(t, err) ensure.DeepEqual(t, len(lazyMD), 1) mdRetrieved, err = lazyMD[0].Get() ensure.Nil(t, err) ensure.Subset(t, *mdRetrieved, SnapshotMetadata{ID: "1", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}}) m.DeleteForReplicaByID("replset1_host1", "1") lazyMD, err = m.getForReplica("replset1_host1") ensure.Nil(t, err) // Both LazySnapshotMetadatas should still be in MetadataStore, but the first one should be marked for delete. ensure.DeepEqual(t, len(lazyMD), 2) ensure.True(t, lazyMD[0].DeleteMark) ensure.False(t, lazyMD[1].DeleteMark) mdRetrieved, err = lazyMD[1].Get() ensure.Nil(t, err) ensure.Subset(t, *mdRetrieved, SnapshotMetadata{ID: "2", Path: "/path/foo", Files: []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}}) }
func TestColumnFamilyOpen(t *testing.T) { dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyOpen") ensure.Nil(t, err) givenNames := []string{"default", "guide"} opts := NewDefaultOptions() opts.SetCreateIfMissingColumnFamilies(true) opts.SetCreateIfMissing(true) db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts}) ensure.Nil(t, err) defer db.Close() ensure.DeepEqual(t, len(cfh), 2) cfh[0].Destroy() cfh[1].Destroy() actualNames, err := ListColumnFamilies(opts, dir) ensure.Nil(t, err) ensure.SameElements(t, actualNames, givenNames) }
func TestHiddenInputs(t *testing.T) { const ( k1 = "k1" v1a = "v1a" v1b = "v1b" k2 = "k2" v2 = "v2" ) v := url.Values{ k1: []string{v1a, v1b}, k2: []string{v2}, } d := HiddenInputs(v).(*Div) ensure.DeepEqual(t, d.Style, "display:none") f := d.Inner.(Frag) ensure.SameElements(t, f, Frag{ &Input{Name: k1, Value: v1a}, &Input{Name: k1, Value: v1b}, &Input{Name: k2, Value: v2}, }) }
func TestGraphObjects(t *testing.T) { var g inject.Graph err := g.Provide( &inject.Object{Value: &TypeNestedStruct{}, Name: "foo"}, &inject.Object{Value: &TypeForGraphObjects{}}, ) ensure.Nil(t, err) ensure.Nil(t, g.Populate()) var actual []string for _, o := range g.Objects() { actual = append(actual, fmt.Sprint(o)) } ensure.SameElements(t, actual, []string{ "*inject_test.TypeAnswerStruct", "*inject_test.TypeForGraphObjects", "*inject_test.TypeNestedStruct named foo", "*inject_test.TypeNestedStruct", `*struct { B *inject_test.TypeNestedStruct "inject:\"\"" }`, }) }
func TestPatternWalker(t *testing.T) { t.Parallel() root := makeEmptyRoot(t) defer os.RemoveAll(root) files, dirs := initProject(t, root) dirs = append(dirs, root) var visitedFiles, visitedDirs []string walkFn := func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { visitedDirs = append(visitedDirs, path) return nil } visitedFiles = append(visitedFiles, path) return nil } testCases := []struct { ignores string expectedFiles, expectedDirs []string }{ { ` test/ !tester/test `, []string{}, []string{ filepath.Join(root, "tester", "inside", "test"), }, }, { ` test !tester/test `, []string{ filepath.Join(root, "tester", "inside", "tester", "test"), }, []string{ filepath.Join(root, "tester", "inside", "test"), }, }, { ` test* `, files, []string{ filepath.Join(root, "tester"), filepath.Join(root, "tester", "inside"), filepath.Join(root, "tester", "inside", "test"), filepath.Join(root, "tester", "inside", "tester"), }, }, } for _, testCase := range testCases { matcher, errors := parseIgnoreMatcher([]byte(testCase.ignores)) ensure.DeepEqual(t, len(errors), 0) visitedFiles, visitedDirs = nil, nil errors, err := parseIgnoreWalk(matcher, root, walkFn) ensure.Nil(t, err) ensure.DeepEqual(t, len(errors), 0) var aggFiles, aggDirs []string aggFiles = append(aggFiles, testCase.expectedFiles...) aggFiles = append(aggFiles, visitedFiles...) aggDirs = append(aggDirs, testCase.expectedDirs...) aggDirs = append(aggDirs, visitedDirs...) ensure.SameElements(t, aggFiles, files) ensure.SameElements(t, aggDirs, dirs) } }
func TestSameElementsIntAndInterface(t *testing.T) { ensure.SameElements(t, []int{1, 2}, []interface{}{2, 1}) }
func TestSubtractFileSets(t *testing.T) { newSet := subtractFileSets( []File{{Name: "a"}, {Name: "b"}, {Name: "c"}}, []File{{Name: "a"}, {Name: "c"}}) ensure.SameElements(t, []File{{Name: "b"}}, newSet) }