示例#1
0
func Example1PushingMessagesToTheQueue() {
	// use the test_queue to push/get messages
	q := mq.New("test_queue")

	q.PushString("Hello, World!")

	// You can also pass multiple messages in a single call.
	q.PushStrings("Message 1", "Message 2")

	// To control parameters like timeout and delay, construct your own message.
	q.PushMessage(&mq.Message{Timeout: 60, Delay: 0, Body: "Hi there"})

	// And finally, all that can be done in bulk as well.
	q.PushMessages(
		&mq.Message{Timeout: 60, Delay: 0, Body: "The first"},
		&mq.Message{Timeout: 60, Delay: 1, Body: "The second"},
		&mq.Message{Timeout: 60, Delay: 2, Body: "The third"},
		&mq.Message{Timeout: 60, Delay: 3, Body: "The fifth"},
	)

	p("all pushed")

	// Output:
	// all pushed
}
示例#2
0
func Example3DeleteMessagesFromTheQueue() {
	q := mq.New("test_queue")
	msg, err := q.Get()
	p(err)
	msg.Delete()

	// Output:
	// <nil>
}
示例#3
0
文件: mq_test.go 项目: szqh97/iron_go
func TestUrl(t *testing.T) {
	fmt.Println("Testing URL with spaces")
	mq := mq.New("MyProject - Prod")
	id, err := mq.PushString("hello")
	if err != nil {
		t.Fatal("No good", err)
	}
	fmt.Println("id:", id)
	info, err := mq.Info()
	if err != nil {
		fmt.Println("ERROR:", err)
		t.Fatal("No good", err)
	}
	fmt.Println(info)
}
示例#4
0
func Example1PushingMessagesToTheQueue() {
	// use a queue named "test_queue" to push/get messages
	q := mq.New("test_queue")

	total := 0

	id, err := q.PushString("Hello, World!")
	assert(err == nil, err)
	assert(len(id) > 1, len(id))
	total++

	// You can also pass multiple messages in a single call.
	ids, err := q.PushStrings("Message 1", "Message 2")
	assert(err == nil, err)
	assert(len(ids) == 2, len(ids))
	total += len(ids)

	// To control parameters like timeout and delay, construct your own message.
	id, err = q.PushMessage(&mq.Message{Timeout: 60, Delay: 0, Body: "Hi there"})
	assert(err == nil, err)
	assert(len(id) > 10, len(id))
	total++

	// And finally, all that can be done in bulk as well.
	ids, err = q.PushMessages(
		&mq.Message{Timeout: 60, Delay: 0, Body: "The first"},
		&mq.Message{Timeout: 60, Delay: 0, Body: "The second"},
		&mq.Message{Timeout: 60, Delay: 0, Body: "The third"},
		&mq.Message{Timeout: 60, Delay: 0, Body: "The fifth"},
	)
	assert(err == nil, err)
	assert(len(ids) == 4, len(ids))
	total += len(ids)

	p("pushed a total of", total, "messages")

	// Output:
	// pushed a total of 8 messages
}
示例#5
0
func Example4ClearQueue() {
	q := mq.New("test_queue")

	info, err := q.Info()

	p(err)
	p("Before Clean(); Name:", info.Name, "Size:", info.Size)

	err = q.Clear()
	p(err)

	info, err = q.Info()

	p(err)
	p("After  Clean(); Name:", info.Name, "Size:", info.Size)

	// Output:
	// <nil>
	// Before Clean(); Name: test_queue Size: 1
	// <nil>
	// <nil>
	// After  Clean(); Name: test_queue Size: 0
}
示例#6
0
func Example2GettingMessagesOffTheQueue() {
	q := mq.New("test_queue")

	// get a single message
	msg, err := q.Get()
	p(err)
	p(msg.Body)

	// get 5 messages
	msgs, err := q.GetN(5)
	p(err)
	p(len(msgs))

	for _, m := range append(msgs, msg) {
		m.Delete()
	}

	// Output:
	// <nil>
	// Hello, World!
	// <nil>
	// 5
}
示例#7
0
func Example2GettingMessagesOffTheQueue() {
	q := mq.New("test_queue")

	// get a single message
	msg, err := q.Get()
	assert(err == nil, err)
	fmt.Printf("The message says: %q\n", msg.Body)

	// when we're done handling a message, we have to delete it, or it
	// will be put back into the queue after a timeout.

	// get 5 messages
	msgs, err := q.GetN(5)
	assert(err == nil, err)
	fmt.Println("Got", len(msgs), "messages from", q.Name)

	for _, m := range append(msgs, msg) {
		m.Delete()
	}

	// Output:
	// The message says: "Hello, World!"
	// Got 5 messages from test_queue
}
示例#8
0
文件: mq_test.go 项目: szqh97/iron_go
func TestEverything(t *testing.T) {
	defer PrintSpecReport()

	qname := "queuename3"

	Describe("IronMQ", func() {
		It("Deletes all existing messages", func() {
			c := mq.New(qname)
			c.PushString("hello") // just to ensure queue exists
			Expect(c.Clear(), ToBeNil)

			info, err := c.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 0x0)
		})

		It("Pushes ands gets a message", func() {
			c := mq.New(qname)
			id1, err := c.PushString("just a little test")
			Expect(err, ToBeNil)
			defer c.DeleteMessage(id1)

			msg, err := c.Get()
			Expect(err, ToBeNil)

			Expect(msg, ToNotBeNil)
			Expect(msg.Id, ToDeepEqual, id1)
			Expect(msg.Body, ToDeepEqual, "just a little test")
		})

		It("clears the queue", func() {
			q := mq.New(qname)

			strings := []string{}
			for n := 0; n < 100; n++ {
				strings = append(strings, fmt.Sprint("test: ", n))
			}

			_, err := q.PushStrings(strings...)
			Expect(err, ToBeNil)

			info, err := q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 100)

			Expect(q.Clear(), ToBeNil)

			info, err = q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 0)
		})

		It("Lists all queues", func() {
			c := mq.New(qname)
			queues, err := c.ListQueues(0, 100) // can't check the caches value just yet.
			Expect(err, ToBeNil)
			found := false
			for _, queue := range queues {
				if queue.Name == qname {
					found = true
					break
				}
			}
			Expect(found, ToEqual, true)
		})

		It("releases a message", func() {
			c := mq.New(qname)

			id, err := c.PushString("trying")
			Expect(err, ToBeNil)

			msg, err := c.Get()
			Expect(err, ToBeNil)

			err = msg.Release(3)
			Expect(err, ToBeNil)

			msg, err = c.Get()
			Expect(msg, ToBeNil)

			time.Sleep(3 * time.Second)

			msg, err = c.Get()
			Expect(err, ToBeNil)
			Expect(msg.Id, ToEqual, id)
		})

		It("updates a queue", func() {
			c := mq.New("pushqueue")
			info, err := c.Info()
			qi := mq.QueueInfo{PushType: "multicast"}
			rc, err := c.Update(qi)
			Expect(err, ToBeNil)
			Expect(info.Id, ToEqual, rc.Id)
		})
		It("Adds and removes subscribers", func() {
			queue := mq.New("addSubscribersTest-" + strconv.Itoa(time.Now().Nanosecond()))
			defer queue.Delete()
			qi := mq.QueueInfo{PushType: "multicast"}
			qi, err := queue.Update(qi)
			Expect(qi.PushType, ToEqual, "multicast")
			Expect(err, ToBeNil)
			err = queue.AddSubscribers("http://server1")
			Expect(err, ToBeNil)
			info, err := queue.Info()
			Expect(err, ToBeNil)
			Expect(len(info.Subscribers), ToEqual, 1)
			err = queue.AddSubscribers("http://server2", "http://server3")
			Expect(err, ToBeNil)
			info, err = queue.Info()
			Expect(err, ToBeNil)
			Expect(len(info.Subscribers), ToEqual, 3)
			err = queue.RemoveSubscribers("http://server2")
			Expect(err, ToBeNil)
			info, err = queue.Info()
			Expect(err, ToBeNil)
			Expect(len(info.Subscribers), ToEqual, 2)
			err = queue.RemoveSubscribers("http://server1", "http://server3")
			Expect(err, ToBeNil)
			info, err = queue.Info()
			Expect(err, ToBeNil)
			Expect(len(info.Subscribers), ToEqual, 0)

		})
	})
}
示例#9
0
func init() {
	defer PrintSpecReport()

	Describe("IronMQ", func() {
		It("Lists all queues", func() {
			c := mq.New("queuename")
			_, err := c.ListQueues(0, 100) // can't check the caches value just yet.
			Expect(err, ToBeNil)
		})

		It("Deletes all existing messages", func() {
			c := mq.New("queuename")

			for {
				info, err := c.Info()
				if err != nil || info.Size == 0 {
					break
				}

				msgs, err := c.GetN(info.Size)
				Expect(err, ToBeNil)

				for _, msg := range msgs {
					err = c.DeleteMessage(msg.Id)
					Expect(err, ToBeNil)
				}
			}
		})

		It("Pushes ands gets a message", func() {
			c := mq.New("queuename")
			id1, err := c.PushString("just a little test")
			Expect(err, ToBeNil)
			defer c.DeleteMessage(id1)

			msg, err := c.Get()
			Expect(err, ToBeNil)

			Expect(msg, ToNotBeNil)
			Expect(msg.Id, ToDeepEqual, id1)
			Expect(msg.Body, ToDeepEqual, "just a little test")
		})

		It("clears the queue", func() {
			q := mq.New("queuename")

			strings := []string{}
			for n := 0; n < 100; n++ {
				strings = append(strings, fmt.Sprint("test: ", n))
			}

			_, err := q.PushStrings(strings...)
			Expect(err, ToBeNil)

			info, err := q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 100)

			err = q.Clear()
			Expect(err, ToBeNil)

			info, err = q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 0)
		})
	})
}
示例#10
0
文件: mq_test.go 项目: edsrzf/iron_go
func init() {
	defer PrintSpecReport()

	Describe("IronMQ", func() {
		It("Deletes all existing messages", func() {
			c := mq.New("queuename")
			Expect(c.Clear(), ToBeNil)

			info, err := c.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 0x0)
		})

		It("Pushes ands gets a message", func() {
			c := mq.New("queuename")
			id1, err := c.PushString("just a little test")
			Expect(err, ToBeNil)
			defer c.DeleteMessage(id1)

			msg, err := c.Get()
			Expect(err, ToBeNil)

			Expect(msg, ToNotBeNil)
			Expect(msg.Id, ToDeepEqual, id1)
			Expect(msg.Body, ToDeepEqual, "just a little test")
		})

		It("clears the queue", func() {
			q := mq.New("queuename")

			strings := []string{}
			for n := 0; n < 100; n++ {
				strings = append(strings, fmt.Sprint("test: ", n))
			}

			_, err := q.PushStrings(strings...)
			Expect(err, ToBeNil)

			info, err := q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 100)

			Expect(q.Clear(), ToBeNil)

			info, err = q.Info()
			Expect(err, ToBeNil)
			Expect(info.Size, ToEqual, 0)
		})

		It("Lists all queues", func() {
			c := mq.New("queuename")
			queues, err := c.ListQueues(0, 100) // can't check the caches value just yet.
			Expect(err, ToBeNil)
			found := false
			for _, queue := range queues {
				if queue.Name == "queuename" {
					found = true
					break
				}
			}
			Expect(found, ToEqual, true)
		})

		It("releases a message", func() {
			c := mq.New("queuename")

			id, err := c.PushString("trying")
			Expect(err, ToBeNil)

			msg, err := c.Get()
			Expect(err, ToBeNil)

			err = msg.Release(3)
			Expect(err, ToBeNil)

			msg, err = c.Get()
			Expect(msg, ToEqual, nil)

			time.Sleep(3)

			msg, err = c.Get()
			Expect(err, ToBeNil)
			Expect(msg.Id, ToEqual, id)
		})
	})
}