Created
April 16, 2020 18:41
-
-
Save louis030195/8f1eb5620507e9e6c84cd71873d37355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/The-Tensox/erutan/cfg" | |
erutan "github.com/The-Tensox/erutan/protobuf" | |
"golang.org/x/net/context" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/credentials" | |
"io" | |
"testing" | |
"time" | |
) | |
func connect(t *testing.T) erutan.Erutan_StreamClient { | |
cfg.Global = cfg.Get() | |
go RunMain() | |
tls := true | |
var opts []grpc.DialOption | |
if tls { | |
creds, err := credentials.NewClientTLSFromFile(cfg.Global.SslCert, "") | |
if err != nil { | |
t.Fatalf("Failed to create TLS credentials %v", err) | |
} | |
opts = append(opts, grpc.WithTransportCredentials(creds)) | |
} else { | |
opts = append(opts, grpc.WithInsecure()) | |
} | |
opts = append(opts, grpc.WithBlock()) | |
conn, err := grpc.Dial(fmt.Sprintf("%s:%s", cfg.Global.Server.Host, cfg.Global.Server.Port), opts...) | |
if err != nil { | |
t.Fatalf("fail to dial: %v", err) | |
} | |
defer conn.Close() | |
client := erutan.NewErutanClient(conn) | |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
defer cancel() | |
c, err := client.Stream(ctx) | |
if err != nil { | |
t.Fatalf("Couldn't open stream : %v", err) | |
} | |
return c | |
} | |
func TestClient(t *testing.T) { | |
c := connect(t) | |
for { | |
p, err := c.Recv() | |
if err == io.EOF { | |
// read done. | |
return | |
} | |
if err != nil { | |
t.Fatalf("Failed to receive : %v", err) | |
} | |
time.Sleep(1*time.Second) | |
t.Logf("%v", p) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment