Example #1
0
func main() {
	socket, err := thrift.NewTSocket("localhost:9090")
	if err != nil {
		panic(err)
	}

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	transport := transportFactory.GetTransport(socket)
	client := contact.NewContactSvcClientFactory(transport, protocolFactory)
	defer client.Transport.Close()
	if err := client.Transport.Open(); err != nil {
		panic(err)
	}

	c1 := contact.NewContactInit("Bob", "111-1111", "*****@*****.**")
	c1, err = client.Create(c1)
	if err != nil {
		panic(err)
	}

	c2, err := client.Read(c1.Id)
	if err != nil {
		panic(err)
	}
	fmt.Println(c2)
}
Example #2
0
func TestClientSpec(t *testing.T) {
	Convey("testing RPC client", t, func() {
		Convey("should be able to create contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2, err := client.Create(contact1)
			So(err, ShouldBeNil)
			So(contact2.Id, ShouldEqual, contact1.Id)
		})

		Convey("should be able to read contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			client.Create(contact1)

			contact2, err := client.Read(contact1.Id)
			So(err, ShouldBeNil)
			So(contact1.Id, ShouldEqual, contact2.Id)
		})

		Convey("should be able to update contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2, err := client.Create(contact1)
			So(err, ShouldBeNil)

			contact2.Name = "alice"
			client.Update(contact2)

			contact3, _ := client.Read(contact1.Id)
			So(contact2.Name, ShouldEqual, contact3.Name)
		})

		Convey("should be able to fetch contacts", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2 := contact.NewContactInit("alice", "222-22222", "*****@*****.**")
			client.Create(contact1)
			client.Create(contact2)

			contacts, err := client.Fetch()
			So(err, ShouldBeNil)
			So(len(contacts), ShouldEqual, 2)
		})

		Convey("should be able to destroy contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			client.Create(contact1)
			client.Destroy(contact1.Id)

			contact2, err := client.Read(contact1.Id)
			So(err, ShouldNotBeNil)
			So(contact2, ShouldBeNil)
		})

		Reset(func() {
			client.Reset()
		})
	})

	server.Stop()
}
Example #3
0
func TestHandlerSpec(t *testing.T) {
	handler := phonebook.NewContactHandler()

	Convey("testing handler", t, func() {
		Convey("should be able to create contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2, err := handler.Create(contact1)
			So(err, ShouldBeNil)
			So(contact1.Id, ShouldEqual, contact2.Id)

			contacts, err := handler.Fetch()
			So(err, ShouldBeNil)
			So(len(contacts), ShouldEqual, 1)
		})

		Convey("should be able to read contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			handler.Create(contact1)

			contact2, err := handler.Read(contact1.Id)
			So(err, ShouldBeNil)
			So(contact1.Id, ShouldEqual, contact2.Id)
		})

		Convey("should be able to update contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2, _ := handler.Create(contact1)

			contact2.Name = "alice"
			handler.Update(contact2)

			contact3, _ := handler.Read(contact1.Id)
			So(contact2.Name, ShouldEqual, contact3.Name)
		})

		Convey("should be able to destroy contact", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			handler.Create(contact1)
			handler.Destroy(contact1.Id)

			contact2, err := handler.Read(contact1.Id)
			So(err, ShouldNotBeNil)
			So(contact2, ShouldBeNil)
		})

		Convey("should be able to fetch contacts", func() {
			contact1 := contact.NewContactInit("bob", "111-1111", "*****@*****.**")
			contact2 := contact.NewContactInit("alice", "222-22222", "*****@*****.**")
			handler.Create(contact1)
			handler.Create(contact2)

			contacts, err := handler.Fetch()
			So(err, ShouldBeNil)
			So(len(contacts), ShouldEqual, 2)
		})

		Reset(func() {
			handler.Reset()
		})
	})
}