func runTest( t *testing.T, driver drive.Driver, f func(t *testing.T, apiClient pfs.ApiClient), ) { grpctest.Run( t, testNumServers, func(servers map[string]*grpc.Server) { discoveryClient := discovery.NewMockClient() i := 0 addresses := make([]string, testNumServers) for address := range servers { shards := make([]string, testShardsPerServer) for j := 0; j < testShardsPerServer; j++ { shards[j] = fmt.Sprintf("%d", (i*testShardsPerServer)+j) } _ = discoveryClient.Set(address+"-master", strings.Join(shards, ",")) addresses[i] = address i++ } _ = discoveryClient.Set("all-addresses", strings.Join(addresses, ",")) for address, server := range servers { combinedAPIServer := NewCombinedAPIServer( route.NewSharder( testShardsPerServer*testNumServers, ), route.NewRouter( route.NewDiscoveryAddresser( discoveryClient, ), route.NewDialer(), address, ), driver, ) pfs.RegisterApiServer(server, combinedAPIServer) pfs.RegisterInternalApiServer(server, combinedAPIServer) } }, func(t *testing.T, clientConns map[string]*grpc.ClientConn) { var clientConn *grpc.ClientConn for _, c := range clientConns { clientConn = c break } for _, c := range clientConns { if c != clientConn { _ = c.Close() } } f( t, pfs.NewApiClient( clientConn, ), ) }, ) }
func do() error { runtime.GOMAXPROCS(runtime.NumCPU()) appEnv := &appEnv{} if err := env.Populate(appEnv, env.PopulateOptions{}); err != nil { return err } if appEnv.NumShards == 0 { appEnv.NumShards = defaultNumShards } var btrfsAPI btrfs.API switch appEnv.BtrfsDriverType { case "exec": btrfsAPI = btrfs.NewExecAPI() case "ffi": fallthrough default: btrfsAPI = btrfs.NewFFIAPI() } address := fmt.Sprintf("0.0.0.0:%d", appEnv.APIPort) combinedAPIServer := server.NewCombinedAPIServer( route.NewSharder( appEnv.NumShards, ), route.NewRouter( route.NewSingleAddresser( address, appEnv.NumShards, ), route.NewDialer(), address, ), drive.NewBtrfsDriver( appEnv.BtrfsRoot, btrfsAPI, ), ) server := grpc.NewServer(grpc.MaxConcurrentStreams(math.MaxUint32)) pfs.RegisterApiServer(server, combinedAPIServer) pfs.RegisterInternalApiServer(server, combinedAPIServer) listener, err := net.Listen("tcp", fmt.Sprintf(":%d", appEnv.APIPort)) if err != nil { return err } errC := make(chan error) go func() { errC <- server.Serve(listener) }() go func() { errC <- http.ListenAndServe(":8080", nil) }() if appEnv.TracePort != 0 { go func() { errC <- http.ListenAndServe(fmt.Sprintf(":%d", appEnv.TracePort), nil) }() } return <-errC }