Пример #1
0
func TestOffload(t *testing.T) {
	tests := []struct {
		In  string
		Out string
	}{
		{"abcd", "dcba"},
		{"racecar", "racecar"},
		{"", ""},
	}

	protos := []webrpc.Protocol{
		webrpc.JSON,
		webrpc.ProtoBuf,
	}

	flag.Parse()
	go main()

	url, err := url.Parse(*base)
	if err != nil {
		log.Fatalf("url: %s", err)
	}

	do := func(pro webrpc.Protocol, s string) string {
		in := &offload.DataSet{Data: &s}
		out := &offload.ResultSet{}

		off := offload.NewOffloadServiceWebClient(pro, url)
		if err := off.Compute(in, out); err != nil {
			log.Fatalf("compute(%q) - %s", s, err)
		}
		if out.Result == nil {
			t.Errorf("compute(%q) returned no result", s)
		}
		return *out.Result
	}

	for _, test := range tests {
		for _, pro := range protos {
			if got, want := do(pro, test.In), test.Out; got != want {
				t.Errorf("compute[%s](%q) = %q, want %q", pro, test.In, got, want)
			}
		}
	}
}
Пример #2
0
func main() {
	url, err := url.Parse(*base)
	if err != nil {
		log.Fatalf("url: %s", err)
	}

	oops := "oops, something bad happened"
	do := func(pro webrpc.Protocol, s string) string {
		in := &offload.DataSet{Data: &s}
		out := &offload.ResultSet{Result: &oops}

		off := offload.NewOffloadServiceWebClient(pro, url)
		if err := off.Compute(in, out); err != nil {
			log.Fatalf("compute(%q) - %s", s, err)
		}
		return *out.Result
	}

	log.Printf(do(webrpc.JSON, "I'm a reversed JSON request"))
	log.Printf(do(webrpc.ProtoBuf, "I am sent as a protobuf"))
}