Skip to content

Instantly share code, notes, and snippets.

@louis030195
Created April 16, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louis030195/8f1eb5620507e9e6c84cd71873d37355 to your computer and use it in GitHub Desktop.
Save louis030195/8f1eb5620507e9e6c84cd71873d37355 to your computer and use it in GitHub Desktop.
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