server_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2021 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package server
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "net"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/zhenghaoz/gorse/config"
  23. "github.com/zhenghaoz/gorse/protocol"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/credentials/insecure"
  26. )
  27. type mockMaster struct {
  28. protocol.UnimplementedMasterServer
  29. addr chan string
  30. grpcServer *grpc.Server
  31. meta *protocol.Meta
  32. cacheTempFile string
  33. dataTempFile string
  34. }
  35. func newMockMaster(t *testing.T) *mockMaster {
  36. cfg := config.GetDefaultConfig()
  37. cfg.Database.DataStore = fmt.Sprintf("sqlite://%s/data.db", t.TempDir())
  38. cfg.Database.CacheStore = fmt.Sprintf("sqlite://%s/cache.db", t.TempDir())
  39. bytes, err := json.Marshal(cfg)
  40. assert.NoError(t, err)
  41. return &mockMaster{
  42. addr: make(chan string),
  43. meta: &protocol.Meta{Config: string(bytes)},
  44. dataTempFile: cfg.Database.DataStore,
  45. cacheTempFile: cfg.Database.CacheStore,
  46. }
  47. }
  48. func (m *mockMaster) GetMeta(_ context.Context, _ *protocol.NodeInfo) (*protocol.Meta, error) {
  49. return m.meta, nil
  50. }
  51. func (m *mockMaster) GetRankingModel(_ *protocol.VersionInfo, _ protocol.Master_GetRankingModelServer) error {
  52. panic("not implement")
  53. }
  54. func (m *mockMaster) GetClickModel(_ *protocol.VersionInfo, _ protocol.Master_GetClickModelServer) error {
  55. panic("not implement")
  56. }
  57. func (m *mockMaster) Start(t *testing.T) {
  58. listen, err := net.Listen("tcp", "localhost:0")
  59. assert.NoError(t, err)
  60. m.addr <- listen.Addr().String()
  61. var opts []grpc.ServerOption
  62. m.grpcServer = grpc.NewServer(opts...)
  63. protocol.RegisterMasterServer(m.grpcServer, m)
  64. err = m.grpcServer.Serve(listen)
  65. assert.NoError(t, err)
  66. }
  67. func (m *mockMaster) Stop() {
  68. m.grpcServer.Stop()
  69. }
  70. func TestServer_Sync(t *testing.T) {
  71. master := newMockMaster(t)
  72. go master.Start(t)
  73. address := <-master.addr
  74. conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
  75. assert.NoError(t, err)
  76. serv := &Server{
  77. testMode: true,
  78. masterClient: protocol.NewMasterClient(conn),
  79. RestServer: RestServer{
  80. Settings: config.NewSettings(),
  81. },
  82. }
  83. serv.Sync()
  84. assert.Equal(t, master.dataTempFile, serv.dataPath)
  85. assert.Equal(t, master.cacheTempFile, serv.cachePath)
  86. assert.NoError(t, serv.DataClient.Close())
  87. assert.NoError(t, serv.CacheClient.Close())
  88. master.Stop()
  89. }