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

Configure Feed

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

1package rpc_test 2 3import ( 4 "context" 5 "net/http/httptest" 6 "net/url" 7 "reflect" 8 "testing" 9 10 "github.com/google/go-cmp/cmp" 11 "github.com/google/go-cmp/cmp/cmpopts" 12 "github.com/sourcegraph/zoekt" 13 "github.com/sourcegraph/zoekt/internal/mockSearcher" 14 "github.com/sourcegraph/zoekt/query" 15 "github.com/sourcegraph/zoekt/rpc" 16) 17 18func TestClientServer(t *testing.T) { 19 mock := &mockSearcher.MockSearcher{ 20 WantSearch: query.NewAnd(mustParse("hello world|universe"), query.NewSingleBranchesRepos("HEAD", 1, 2)), 21 SearchResult: &zoekt.SearchResult{ 22 Files: []zoekt.FileMatch{ 23 {FileName: "bin.go"}, 24 }, 25 }, 26 27 WantList: &query.Const{Value: true}, 28 RepoList: &zoekt.RepoList{ 29 Repos: []*zoekt.RepoListEntry{ 30 { 31 Repository: zoekt.Repository{ 32 ID: 2, 33 Name: "foo/bar", 34 }, 35 }, 36 }, 37 }, 38 } 39 40 ts := httptest.NewServer(rpc.Server(mock)) 41 defer ts.Close() 42 43 u, err := url.Parse(ts.URL) 44 if err != nil { 45 t.Fatal(err) 46 } 47 client := rpc.Client(u.Host) 48 defer client.Close() 49 50 var cached query.Q = &query.GobCache{ 51 Q: mock.WantSearch, 52 } 53 54 r, err := client.Search(context.Background(), cached, &zoekt.SearchOptions{}) 55 if err != nil { 56 t.Fatal(err) 57 } 58 if !reflect.DeepEqual(r, mock.SearchResult) { 59 t.Fatalf("got %+v, want %+v", r, mock.SearchResult) 60 } 61 62 l, err := client.List(context.Background(), mock.WantList, nil) 63 if err != nil { 64 t.Fatal(err) 65 } 66 if d := cmp.Diff(mock.RepoList, l, cmpopts.IgnoreUnexported(zoekt.Repository{})); d != "" { 67 t.Fatalf("unexpected RepoList (-want, +got):\n%s", d) 68 } 69 70 // Test closing a client we never dial. 71 noopClient := rpc.Client(u.Host) 72 noopClient.Close() 73} 74 75func mustParse(s string) query.Q { 76 q, err := query.Parse(s) 77 if err != nil { 78 panic(err) 79 } 80 return q 81}