How do I test microservices in go-micro
-
Okay. So I'm learning Go. And I'm learning this Micro thing. I've got a test file for my method in Go. But I can't run it anymore now that I've introduced micro. What am I doing wrong?
I run
go test
and I getcan't load package: package rot13: found packages main (rot13.go) and rot13 (rot13.pb.go) in /home/ubuntu/workspace/src/rot13
Here's my test:
package main import ( "testing" ) func TestEncodeReturnsString(t *testing.T) { /* table based test case*/ table := []struct { input string expected string }{ /* Easy case: first half of alphabet*/ {"aabbcc", "nnoopp"}, /* Harder case: last half of alphabet*/ {"nnoopp", "aabbcc"}, /*Capital letters: first half*/ {"AABBCC", "NNOOPP"}, /*Capital letters: last half*/ {"NNOOPP", "AABBCC"}, /*Non-letters*/ {"@?_~[{|", "@?_~[{|"}, } for _, test := range table { output := Encode(test.input) if output != test.expected { t.Errorf("Incorrect string returned for input %s; expected %s but got %s", test.input, test.expected, output) } } }
My function:
func EncodeRot13(s string) string { var output []byte for i := 0; i < len(s); i++ { if s[i] >= 97 && s[i] <= 109 || s[i] >= 65 && s[i] <= 77 { output = append(output, s[i]+13) } else if s[i] >= 110 && s[i] <= 122 || s[i] >= 78 && s[i] <= 90 { output = append(output, s[i]-13) } else { output = append(output, s[i]) } } return string(output[:]) }
And all the extra Micro crap:
func main() { service := micro.NewService( micro.Name("rot13"), ) service.Init() service.Run() } type Rot13 struct{} func (g *Rot13) Encode(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error { rsp.output = EncodeRot13(req.input) return nil }
Including the... uh... proto file I guess?
syntax = "proto3"; service Rot13 { rpc Encode(EncodeRequest) returns (EncodeResponse) {} } message EncodeRequest { string input = 1; } message EncodeResponse { string output = 2; }
Halp I have no idea what I'm doing~!
-
@Yamikuronue Oh yeah: @ben_lubar for maybe having a clue
-
Your files all have to have the same package if they're in the same folder. You have main on rot13.go but rot13 on rot13.pb.go.
Tests can either be in the same package as the package they're testing or can have _test appended if they want to test the public API only.
-
@ben_lubar I was told having package main is what makes something an entry point? Weird.
-
@Yamikuronue said in How do I test microservices in go-micro:
@ben_lubar I was told having package main is what makes something an entry point? Weird.
Yeah, package main with a function named main is your entry point. The other files in the same folder have to be part of the same package main, but if you make a subfolder you can give them a different package that can be imported.
Alternatively, you can add
// +build ignore
to the top of your main file and compile it withgo build main_file_name.go
if you want to keep it in the same directory.
-
@ben_lubar rot12.pb.go was auto-generated so.... what should I have done here?
-
@Yamikuronue said in How do I test microservices in go-micro:
@ben_lubar rot12.pb.go was auto-generated so.... what should I have done here?
I think if you put
package main;
at the top of the protocol buffers definition file it'll compile to the main package. Otherwise you can just edit the first line of rot13.pb.go or put it in a subfolder.
-
Well, if all you're testing is the interface of microservices using protocol buffers, you don't need to use go at all. Just saying…
-
@asdf My tests are of the business logic.
-
@Yamikuronue said in How do I test microservices in go-micro:
My tests are of the business logic.
Ah, OK. Bad luck for you, then. ;)
-
Also, if you're using Go 1.7 or later:
-
@ben_lubar Okay, I did that. But I can't import it because it's not importable, but if I don't import it, the stuff defined automatically by the protocol buffers shit doesn't work in my program. So. What did I do wrong?
My code:
Compare to the example code:
https://github.com/micro/examples/tree/master/greeter/srv
-
@Yamikuronue add
proto "github.com/micro/examples/greeter/srv/proto/hello"
to your import block at the top.
-
@ben_lubar that means something like
from github.com/micro/examples/greeter/srv/proto/hello import proto
right? I want to import proto, but from my localproto
directory undersrc/rot13
instead. How do I do that? It yells at me thatimport "rot13" is a program, not an importable package
-
@Yamikuronue
"github.com/yamikuronue/go-rot13-practice/src/rot13/proto"
?
-
@ben_lubar said in How do I test microservices in go-micro:
github.com/yamikuronue/go-rot13-practice/src/proto/proto
I can't import the local copy? What will that mean in practical terms -- will it fetch out to github every time I compile? Will it try to install the dependency from github?
-
main.go:6:2: cannot find package "github.com/yamikuronue/go-rot13-practice/src/rot13/proto" in any of: /opt/go/src/github.com/yamikuronue/go-rot13-practice/src/rot13/proto (from $GOROOT) /home/ubuntu/workspace/src/github.com/yamikuronue/go-rot13-practice/src/rot13/proto (from $GOPATH)
I'm in
/home/ubuntu/workspace/src/rot13
with aproto
subdirectory containing the rot13 package, and this directory contains the main package
-
@Yamikuronue said in How do I test microservices in go-micro:
@ben_lubar said in How do I test microservices in go-micro:
github.com/yamikuronue/go-rot13-practice/src/proto/proto
I can't import the local copy? What will that mean in practical terms -- will it fetch out to github every time I compile? Will it try to install the dependency from github?
It means that your local copy will be in a folder named something like
/home/yamikuronue/gopath/src/github.com/yamikuronue/go-rot13-practice/src/rot13
. It will only download anything if you usego get
. The idea is to make the place to get the dependencies part of the code itself.
-
@ben_lubar so it should be `"rot13/proto/rot13"?
-
@Yamikuronue said in How do I test microservices in go-micro:
I'm in
/home/ubuntu/workspace/src/rot13
with aproto
subdirectory containing the rot13 package, and this directory contains the main packageThat would be idiomatic if you owned the top level domain
rot13
.
-
@Yamikuronue said in How do I test microservices in go-micro:
@ben_lubar so it should be `"rot13/proto/rot13"?
Sure, if you don't care about your project being
go get
-able.
-
AHA! I wanted to do
import ( "github.com/micro/go-micro"; "context"; rot13 "rot13/proto" )
@ben_lubar said in How do I test microservices in go-micro:
Sure, if you don't care about your project being go get-able.
I don't.
-
@ben_lubar said in How do I test microservices in go-micro:
That would be idiomatic if you owned the top level domain rot13.
So namespacing works like Java then?
-
@Yamikuronue said in How do I test microservices in go-micro:
@ben_lubar said in How do I test microservices in go-micro:
That would be idiomatic if you owned the top level domain rot13.
So namespacing works like Java then?
Except for Java having reversed domain names, yes.
-
@Yamikuronue said in How do I test microservices in go-micro:
AHA! I wanted to do
import ( "github.com/micro/go-micro"; "context"; rot13 "rot13/proto" )
Ew, run
go fmt
on that!
-
@ben_lubar said in How do I test microservices in go-micro:
Except for Java having reversed domain names, yes.
Go namespaces names run from most specific to most general?
-
@dkf said in How do I test microservices in go-micro:
@ben_lubar said in How do I test microservices in go-micro:
Except for Java having reversed domain names, yes.
Go namespaces names run from most specific to most general?
Go
namespacespackages have the entire domain name as one folder:import "google.golang.org/appengine/memcache"
-
That awkward moment when you google for more information and find your own thread in the top ten.
-
@Yamikuronue said in How do I test microservices in go-micro:
That awkward moment when you google for more information and find your own thread in the top ten.
the more people i watch get introduced to
go
the more i wonder why the language exists..... it really seems to be poorly designed based on the amount of trouble people have solving their problems in it the instant they walk away from thehello world
tutorial.....there's got to be something there, but fuck me with a purple phalus if i can understand it....
-
@Yamikuronue
One of my new co-workers had a few questions about a framework we're using. All his Google searches lead to forum threads where I asked the same question and got no official answer from the devs. That taught him to ask his more experienced co-workers for advice instead of searching on the internet for hours. :D
-
Ok, new question: How come the hello world example has a whole section about the server and client for the Greeter service, but I don't have that in my rot13.pb.go? Just the data types. I must have screwed up the proto file somehow, but how?
-
@Yamikuronue Because the generator put the output in the wrong folder, and didn't overwrite the (very old) current version
-
@accalia said in How do I test microservices in go-micro:
the more people i watch get introduced to go the more i wonder why the language exists...
It's got very good inter-
threadgoroutine communication semantics. Pity about the other stupids.