fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

shards: add test for no shards loading and crashes (#518)

This is repentance for introducing this bug and others fixing it. This
adds a test to ensure we start up correctly if there are no shards to
load.

Test Plan: Revert the fix 54c8cf0c and check that test fails.

+82
+82
shards/shards_test.go
··· 1057 1057 sres, _ := ss.Search(context.Background(), q, &zoekt.SearchOptions{}) 1058 1058 return sres.Files 1059 1059 } 1060 + 1061 + // Ensure we work on empty shard directories. 1062 + func TestNewDirectorySearcher_empty(t *testing.T) { 1063 + ctx := context.Background() 1064 + 1065 + test := func(t *testing.T, ss zoekt.Streamer) { 1066 + res, err := ss.Search(ctx, &query.Const{Value: true}, nil) 1067 + if err != nil { 1068 + t.Fatal("Search non-nil error", err) 1069 + } 1070 + 1071 + if diff := cmp.Diff(&zoekt.SearchResult{}, res, cmpopts.IgnoreFields(zoekt.Stats{}, "Duration", "Wait"), cmpopts.EquateEmpty()); diff != "" { 1072 + t.Fatalf("Search had non empty results (-want, +got):\n%s", diff) 1073 + } 1074 + 1075 + rl, err := ss.List(ctx, &query.Const{Value: true}, nil) 1076 + if err != nil { 1077 + t.Fatal("List non-nil error", err) 1078 + } 1079 + if diff := cmp.Diff(&zoekt.RepoList{}, rl, cmpopts.EquateEmpty()); diff != "" { 1080 + t.Fatalf("List had non empty results (-want, +got):\n%s", diff) 1081 + } 1082 + } 1083 + 1084 + dir := t.TempDir() 1085 + t.Run("blocking", func(t *testing.T) { 1086 + ss, err := NewDirectorySearcher(dir) 1087 + if err != nil { 1088 + t.Fatal(err) 1089 + } 1090 + t.Cleanup(ss.Close) 1091 + 1092 + // We expect crashes to be empty as soon as NewDirectorySearcher returns 1093 + // so we can validate straight away. 1094 + test(t, ss) 1095 + }) 1096 + 1097 + t.Run("fast", func(t *testing.T) { 1098 + ss, err := NewDirectorySearcherFast(dir) 1099 + if err != nil { 1100 + t.Fatal(err) 1101 + } 1102 + t.Cleanup(ss.Close) 1103 + 1104 + deadline := testDeadline(t, 10*time.Second) 1105 + 1106 + // Wait for scanning of directory to be done. We should be returning 1107 + // non-zero crashes until then. 1108 + waitForPredicate(deadline, 10*time.Millisecond, func() bool { 1109 + res, err := ss.Search(ctx, &query.Const{Value: true}, nil) 1110 + if err != nil { 1111 + t.Fatal(err) 1112 + } 1113 + return res.Stats.Crashes == 0 1114 + }) 1115 + 1116 + test(t, ss) 1117 + }) 1118 + } 1119 + 1120 + // testDeadline returns the deadline for t, but ensures it is no longer than 1121 + // maxTimeout away. 1122 + func testDeadline(t *testing.T, maxTimeout time.Duration) time.Time { 1123 + deadline := time.Now().Add(maxTimeout) 1124 + if d, ok := t.Deadline(); ok && d.Before(deadline) { 1125 + // give 1s for us to do a final test run 1126 + deadline = d.Add(-time.Second) 1127 + } 1128 + return deadline 1129 + } 1130 + 1131 + func waitForPredicate(deadline time.Time, tick time.Duration, pred func() bool) bool { 1132 + for time.Now().Before(deadline) { 1133 + if pred() { 1134 + return true 1135 + } 1136 + 1137 + time.Sleep(tick) 1138 + } 1139 + 1140 + return pred() 1141 + }