Beispiel #1
0
func main() {
	short := "h"
	long := []string{
		"help",
		"allow-dups",
	}
	args, optargs, err := getopt.GetOpt(os.Args[1:], short, long)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		Usage(ErrorCodes["opts"])
	}

	port := -1
	dups := false
	for _, oa := range optargs {
		switch oa.Opt() {
		case "-h", "--help":
			Usage(0)
		case "--allow-dups":
			dups = true
		}
	}

	if len(args) != 1 {
		fmt.Fprintln(os.Stderr, "You must specify a port")
		Usage(ErrorCodes["opts"])
	}
	port = parse_int(args[0])

	fmt.Println("starting")
	server := net.NewServer(func() net.Queue { return queue.NewQueue(dups) })
	server.Start(port)
}
Beispiel #2
0
func TestMultiConnection(t *testing.T) {
	server := NewServer(func() Queue { return queue.NewQueue(true) })
	connect := func() (chan<- []byte, <-chan []byte) {
		send := make(chan []byte)
		r := sender_to_reciever(send)
		s := make(chan []byte)
		go server.Connection(s, r).Serve()
		return send, s
	}

	test := func(final bool) {
		send, recv := connect()
		l := make([][]byte, 0, 25)
		for i := 0; i < rand.Intn(25)+10; i++ {
			item := []byte("same item for all!") // that way it doesn't matter
			// who deques
			l = append(l, item)
			send <- EncodeB64Message("ENQUE", item)
			cmd, rest := DecodeCmd(<-recv)
			if cmd != "OK" && rest != nil {
				t.Fatal("Expected an OK response", cmd, string(rest))
			}
			send <- EncodeB64Message("HAS", queue.Hash(item))
			cmd, data := DecodeCmd(<-recv)
			if cmd != "TRUE" && data != nil {
				t.Fatal("Expected an TRUE response")
			}
		}
		for _, item := range l {
			send <- EncodePlainMessage("DEQUE", nil)
			cmd, rest := DecodeCmd(<-recv)
			if cmd != "ITEM" || rest == nil {
				t.Fatal("expected an item")
			}
			q_item, err := DecodeB64(rest)
			if err != nil {
				t.Fatal(err)
			}
			if !bytes.Equal(q_item, item) {
				t.Fatal("items should have equalled each other")
			}
		}
		if final {
			size := 1
			for size > 0 {
				send <- EncodePlainMessage("SIZE", nil)
				cmd, sSize := DecodeCmd(<-recv)
				if cmd != "SIZE" {
					t.Fatal("expected a SIZE")
				}
				i, err := strconv.Atoi(string(bytes.TrimSpace(sSize)))
				if err != nil {
					t.Fatal(err)
				}
				size = i
			}
			send <- EncodePlainMessage("DEQUE", nil)
			cmd, rest := DecodeCmd(<-recv)
			if cmd != "ERROR" {
				t.Fatal("expected an error")
			}
			if rest == nil {
				t.Fatal("expected queue empty message")
			}
			msg, err := DecodeB64(rest)
			if err != nil {
				t.Fatal(err)
			}
			if string(msg) != "queue is empty" {
				t.Fatal("expected queue empty message")
			}
		}
		close(send)
		<-recv
	}
	go test(false)
	go test(false)
	go test(false)
	go test(false)
	test(true)
}
Beispiel #3
0
func TestConnection(t *testing.T) {
	server := NewServer(func() Queue { return queue.NewQueue(true) })
	connect := func() (chan<- []byte, <-chan []byte) {
		send := make(chan []byte)
		r := sender_to_reciever(send)
		s := make(chan []byte)
		go server.Connection(s, r).Serve()
		return send, s
	}

	test := func() {
		send, recv := connect()
		l := make([][]byte, 0, 25)
		for i := 0; i < rand.Intn(25)+10; i++ {
			item := rand_bytes(rand.Intn(32) + 2)
			l = append(l, item)
			send <- EncodeB64Message("ENQUE", item)
			cmd, data := DecodeCmd(<-recv)
			if cmd != "OK" && data != nil {
				t.Fatal("Expected an OK response")
			}
			send <- EncodeB64Message("HAS", queue.Hash(item))
			cmd, data = DecodeCmd(<-recv)
			if cmd != "TRUE" && data != nil {
				t.Fatal("Expected an TRUE response")
			}
		}
		for _, item := range l {
			send <- EncodePlainMessage("DEQUE", nil)
			cmd, rest := DecodeCmd(<-recv)
			if cmd != "ITEM" || rest == nil {
				t.Fatal("expected an item")
			}
			q_item, err := DecodeB64(rest)
			if err != nil {
				t.Fatal(err)
			}
			if !bytes.Equal(q_item, item) {
				t.Fatal("items should have equalled each other")
			}
		}
		send <- EncodePlainMessage("DEQUE", nil)
		cmd, rest := DecodeCmd(<-recv)
		if cmd != "ERROR" {
			t.Fatal("expected an error")
		}
		if rest == nil {
			t.Fatal("expected queue empty message")
		}
		msg, err := DecodeB64(rest)
		if err != nil {
			t.Fatal(err)
		}
		if string(msg) != "queue is empty" {
			t.Fatal("expected queue empty message")
		}
		close(send)
		<-recv
	}
	test()
	test()
	test()
	test()
	test()
}