예제 #1
0
func TestDiskQueueCorruption(t *testing.T) {
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)

	dqName := "test_disk_queue_corruption" + strconv.Itoa(int(time.Now().Unix()))
	dq := NewDiskQueue(dqName, os.TempDir(), 1000, 5)

	msg := make([]byte, 123)
	for i := 0; i < 25; i++ {
		dq.Put(msg)
	}

	assert.Equal(t, dq.Depth(), int64(25))

	// corrupt the 2nd file
	dqFn := dq.(*DiskQueue).fileName(1)
	os.Truncate(dqFn, 500)

	for i := 0; i < 19; i++ {
		assert.Equal(t, <-dq.ReadChan(), msg)
	}

	// corrupt the 4th (current) file
	dqFn = dq.(*DiskQueue).fileName(3)
	os.Truncate(dqFn, 100)

	dq.Put(msg)

	assert.Equal(t, <-dq.ReadChan(), msg)
}
예제 #2
0
func TestNew(t *testing.T) {
	p := New()
	zero := int64(0)
	assert.Equal(t, zero, p.Current)
	assert.Equal(t, zero, p.Total)
	assert.Equal(t, zero, p.Expected)
}
예제 #3
0
func TestProgressReader(t *testing.T) {
	filename := "progress_test.go"
	f, err := os.Open(filename)
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	fs, err := os.Stat(filename)
	if err != nil {
		log.Fatalln(err)
	}

	p := New()
	p.Total = fs.Size()
	p.Progress = func(current, total, expected int64) {
		log.Println("Reading", current, total, expected)
		assert.Equal(t, true, current <= total)
	}

	b := new(bytes.Buffer)
	r := syncreader.New(f, p)
	_, err = b.ReadFrom(r)
	if err != nil {
		log.Fatalln(err)
	}
	assert.Equal(t, fs.Size(), int64(b.Len()))
}
예제 #4
0
func TestDoozerGet(t *testing.T) {
	l := mustListen()
	defer l.Close()
	u := mustListenUDP(l.Addr().String())
	defer u.Close()

	go Main("a", "X", "", "", "", nil, u, l, nil, 1e9, 2e9, 3e9, 101)

	cl := dial(l.Addr().String())

	_, err := cl.Set("/x", store.Missing, []byte{'a'})
	assert.Equal(t, nil, err)

	ents, rev, err := cl.Get("/x", nil)
	assert.Equal(t, nil, err)
	assert.NotEqual(t, store.Dir, rev)
	assert.Equal(t, []byte{'a'}, ents)

	//cl.Set("/test/a", store.Missing, []byte{'1'})
	//cl.Set("/test/b", store.Missing, []byte{'2'})
	//cl.Set("/test/c", store.Missing, []byte{'3'})

	//ents, rev, err = cl.Get("/test", 0)
	//sort.SortStrings(ents)
	//assert.Equal(t, store.Dir, rev)
	//assert.Equal(t, nil, err)
	//assert.Equal(t, []string{"a", "b", "c"}, ents)
}
예제 #5
0
func TestConnectInvalidUrl(t *testing.T) {
	//
	//  Missing protocol scheme - url.Parse should fail
	//
	_, err := Connect("://foobar.com")
	if err == nil {
		t.Fatal("Expected error due to missing protocol scheme")
	}
	//
	// Unsupported protocol scheme - restclient.Do should fail
	//
	_, err = Connect("foo://bar.com")
	if err == nil {
		t.Fatal("Expected error due to unsupported protocol scheme")
	}
	//
	// Not Found
	//
	_, err = Connect("http://localhost:7474/db/datadatadata")
	assert.Equal(t, InvalidDatabase, err)
	//
	// 200 Success and HTML returned
	//
	_, err = Connect("http://localhost:7474")
	assert.Equal(t, InvalidDatabase, err)
}
예제 #6
0
func TestRemove(t *testing.T) {
	c := 100
	pq := newInFlightPqueue(c)

	msgs := make(map[MessageID]*Message)
	for i := 0; i < c; i++ {
		m := &Message{pri: int64(rand.Intn(100000000))}
		copy(m.ID[:], fmt.Sprintf("%016d", m.pri))
		msgs[m.ID] = m
		pq.Push(m)
	}

	for i := 0; i < 10; i++ {
		idx := rand.Intn((c - 1) - i)
		var fm *Message
		for _, m := range msgs {
			if m.index == idx {
				fm = m
				break
			}
		}
		rm := pq.Remove(idx)
		assert.Equal(t, fmt.Sprintf("%s", fm.ID), fmt.Sprintf("%s", rm.ID))
	}

	lastPriority := pq.Pop().pri
	for i := 0; i < (c - 10 - 1); i++ {
		msg := pq.Pop()
		assert.Equal(t, lastPriority <= msg.pri, true)
		lastPriority = msg.pri
	}
}
예제 #7
0
func subFail(t *testing.T, conn io.ReadWriter, topicName string, channelName string) {
	err := nsq.Subscribe(topicName, channelName).Write(conn)
	assert.Equal(t, err, nil)
	resp, err := nsq.ReadResponse(conn)
	frameType, _, err := nsq.UnpackResponse(resp)
	assert.Equal(t, frameType, nsq.FrameTypeError)
}
예제 #8
0
func TestRouterGet(t *testing.T) {
	handlers := map[string]int{}
	router := NewRouter("")
	router.CreateHandle(nopeHandleFunc)

	//handlers["/"] = false
	router.Add("GET", "/", func(ctx *Context) { handlers["/"]++ })
	router.Add("GET", "/users", func(ctx *Context) { handlers["/users"]++ })
	router.Add("GET", "/users/:userId", func(ctx *Context) { handlers["/users/:userId"]++ })
	router.Add("GET", "/users/:userId/sales", func(ctx *Context) { handlers["/users/:userId/sales"]++ })
	router.Add("GET", "/users/:userId/sales/:saleId", func(ctx *Context) { handlers["/users/:userId/sales/:saleId"]++ })

	router.handle(createContext("GET", ""))
	router.handle(createContext("GET", "/users"))
	router.handle(createContext("GET", "/users/1"))
	router.handle(createContext("GET", "/users/1/?order=asc"))
	router.handle(createContext("GET", "/users/sales"))
	router.handle(createContext("GET", "/users/_sales"))
	router.handle(createContext("GET", "/users/1/sales"))
	router.handle(createContext("GET", "/users/1/sales/2"))

	assert.Equal(t, 1, handlers["/"])
	assert.Equal(t, 1, handlers["/users"])
	assert.Equal(t, 3, handlers["/users/:userId"])
	assert.Equal(t, 1, handlers["/users/:userId/sales"])
	assert.Equal(t, 1, handlers["/users/:userId/sales/:saleId"])

}
예제 #9
0
func TestNoRequestSignature(t *testing.T) {
	st := NewSignatureTest()
	defer st.Close()
	st.MakeRequestWithExpectedKey("GET", "", "")
	assert.Equal(t, 200, st.rw.Code)
	assert.Equal(t, st.rw.Body.String(), "no signature received")
}
예제 #10
0
func TestAuthorizationsService_All(t *testing.T) {
	setup()
	defer tearDown()

	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, "GET")
		respondWithJSON(w, loadFixture("authorizations.json"))
	})

	url, err := AuthorizationsURL.Expand(nil)
	assert.Equal(t, nil, err)

	auths, result := client.Authorizations(url).All()
	assert.T(t, !result.HasError())

	firstAuth := auths[0]
	assert.Equal(t, 1, firstAuth.ID)
	assert.Equal(t, "https://api.github.com/authorizations/1", firstAuth.URL)
	assert.Equal(t, "456", firstAuth.Token)
	assert.Equal(t, "", firstAuth.Note)
	assert.Equal(t, "", firstAuth.NoteURL)
	assert.Equal(t, "2012-11-16 01:05:51 +0000 UTC", firstAuth.CreatedAt.String())
	assert.Equal(t, "2013-08-21 03:29:51 +0000 UTC", firstAuth.UpdatedAt.String())

	app := App{ClientID: "123", URL: "http://localhost:8080", Name: "Test"}
	assert.Equal(t, app, firstAuth.App)

	assert.Equal(t, 2, len(firstAuth.Scopes))
	scopes := []string{"repo", "user"}
	assert.T(t, reflect.DeepEqual(firstAuth.Scopes, scopes))
}
예제 #11
0
func TestAuthorizationsService_Create(t *testing.T) {
	setup()
	defer tearDown()

	params := AuthorizationParams{Scopes: []string{"public_repo"}}

	mux.HandleFunc("/authorizations", func(w http.ResponseWriter, r *http.Request) {
		var authParams AuthorizationParams
		json.NewDecoder(r.Body).Decode(&authParams)
		assert.T(t, reflect.DeepEqual(authParams, params))

		testMethod(t, r, "POST")
		respondWithJSON(w, loadFixture("create_authorization.json"))
	})

	url, err := AuthorizationsURL.Expand(nil)
	assert.Equal(t, nil, err)

	auth, _ := client.Authorizations(url).Create(params)

	assert.Equal(t, 3844190, auth.ID)
	assert.Equal(t, "https://api.github.com/authorizations/3844190", auth.URL)
	assert.Equal(t, "123", auth.Token)
	assert.Equal(t, "", auth.Note)
	assert.Equal(t, "", auth.NoteURL)
	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.CreatedAt.String())
	assert.Equal(t, "2013-09-28 18:44:39 +0000 UTC", auth.UpdatedAt.String())

	app := App{ClientID: "00000000000000000000", URL: "http://developer.github.com/v3/oauth/#oauth-authorizations-api", Name: "GitHub API"}
	assert.Equal(t, app, auth.App)

	assert.Equal(t, 1, len(auth.Scopes))
	scopes := []string{"public_repo"}
	assert.T(t, reflect.DeepEqual(auth.Scopes, scopes))
}
예제 #12
0
func TestInFlightWorker(t *testing.T) {
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)

	options := NewNsqdOptions()
	options.msgTimeout = 300 * time.Millisecond
	nsqd = NewNSQd(1, options)
	defer nsqd.Exit()

	topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
	topic := nsqd.GetTopic(topicName)
	channel := topic.GetChannel("channel")

	for i := 0; i < 1000; i++ {
		msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
		channel.StartInFlightTimeout(msg, NewClientV2(nil))
	}

	assert.Equal(t, len(channel.inFlightMessages), 1000)
	assert.Equal(t, len(channel.inFlightPQ), 1000)

	time.Sleep(350 * time.Millisecond)

	assert.Equal(t, len(channel.inFlightMessages), 0)
	assert.Equal(t, len(channel.inFlightPQ), 0)
}
예제 #13
0
func TestProcessCounters(t *testing.T) {

	Config.PersistCountKeys = int64(10)
	counters = make(map[string]int64)
	var buffer bytes.Buffer
	now := int64(1418052649)

	counters["gorets"] = int64(123)

	var err error
	Config.StoreDb = "/tmp/stats_test.db"
	removeFile(Config.StoreDb)

	dbHandle, err = bolt.Open(Config.StoreDb, 0644, &bolt.Options{Timeout: 1 * time.Second})
	if err != nil {
		log.Fatalf("Error opening %s (%s)\n", Config.StoreDb, err)
	}

	defer closeAndRemove(dbHandle, Config.StoreDb)

	num := processCounters(&buffer, now, true, "external", dbHandle)
	assert.Equal(t, num, int64(1))
	assert.Equal(t, buffer.String(), "gorets 123 1418052649\n")

	// run processCounters() enough times to make sure it purges items
	for i := 0; i < int(Config.PersistCountKeys)+10; i++ {
		num = processCounters(&buffer, now, true, "external", dbHandle)
	}
	lines := bytes.Split(buffer.Bytes(), []byte("\n"))

	// expect two more lines - the good one and an empty one at the end
	assert.Equal(t, len(lines), int(Config.PersistCountKeys+2))
	assert.Equal(t, string(lines[0]), "gorets 123 1418052649")
	assert.Equal(t, string(lines[Config.PersistCountKeys]), "gorets 0 1418052649")
}
예제 #14
0
func TestInFlightWorker(t *testing.T) {
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)

	options := NewNSQDOptions()
	options.MsgTimeout = 200 * time.Millisecond
	nsqd := NewNSQD(options)
	defer nsqd.Exit()

	topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
	topic := nsqd.GetTopic(topicName)
	channel := topic.GetChannel("channel")

	for i := 0; i < 1000; i++ {
		msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
		channel.StartInFlightTimeout(msg, 0)
	}

	assert.Equal(t, len(channel.inFlightMessages), 1000)
	assert.Equal(t, len(channel.inFlightPQ), 1000)

	// the in flight worker has a resolution of 100ms so we need to wait
	// at least that much longer than our msgTimeout (in worst case)
	time.Sleep(options.MsgTimeout + 100*time.Millisecond)

	assert.Equal(t, len(channel.inFlightMessages), 0)
	assert.Equal(t, len(channel.inFlightPQ), 0)
}
예제 #15
0
func TestPathWillAugmentExisting(t *testing.T) {
	js, err := NewJson([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`))
	assert.Equal(t, nil, err)

	js.SetPath([]string{"this", "d"}, "dd")

	cases := []struct {
		path    []string
		outcome string
	}{
		{
			path:    []string{"this", "a"},
			outcome: "aa",
		},
		{
			path:    []string{"this", "b"},
			outcome: "bb",
		},
		{
			path:    []string{"this", "c"},
			outcome: "cc",
		},
		{
			path:    []string{"this", "d"},
			outcome: "dd",
		},
	}

	for _, tc := range cases {
		s, err := js.GetPath(tc.path...).String()
		assert.Equal(t, nil, err)
		assert.Equal(t, tc.outcome, s)
	}
}
예제 #16
0
func TestConsumer_Start_Handler(t *testing.T) {
	done := make(chan bool)
	b := new(bytes.Buffer)
	l := log.New(b, "", 0)

	c := queue.NewConsumer("events", "ingestion")
	c.SetLogger(l, nsq.LogLevelDebug)

	c.Set("nsqd", ":5001")
	c.Set("nsqds", []interface{}{":5001"})
	c.Set("concurrency", 5)
	c.Set("max_attempts", 10)
	c.Set("max_in_flight", 150)
	c.Set("default_requeue_delay", "15s")

	err := c.Start(nsq.HandlerFunc(func(msg *nsq.Message) error {
		done <- true
		return nil
	}))

	assert.Equal(t, nil, err)

	go func() {
		p, err := nsq.NewProducer(":5001", nsq.NewConfig())
		check(err)
		p.Publish("events", []byte("hello"))
	}()

	<-done
	assert.Equal(t, nil, c.Stop())
}
예제 #17
0
func TestLowerPercentile(t *testing.T) {
	// Some data with expected mean of 20
	d := []byte("time:0|ms\ntime:1|ms\ntime:2|ms\ntime:3|ms")
	packets := udp.ParseMessage(d, prefix_internal, output, udp.ParseLine)

	pct, _ := timers.NewPercentiles("-75")
	ti := timers.New("", *pct)

	for _, p := range packets {
		ti.Add(p)
	}

	var buff bytes.Buffer
	var num int64
	num += ti.Process(&buff, time.Now().Unix(), 10)

	assert.Equal(t, num, int64(1))
	dataForGraphite := buff.String()

	meanRegexp := regexp.MustCompile(`time\.upper_75 1\.`)
	matched := meanRegexp.MatchString(dataForGraphite)
	assert.Equal(t, matched, false)

	meanRegexp = regexp.MustCompile(`time\.lower_75 1\.`)
	matched = meanRegexp.MatchString(dataForGraphite)
	assert.Equal(t, matched, true)
}
예제 #18
0
func TestCountersLegacyNamespaceFalse(t *testing.T) {
	cnt := counters.New("rates.", "counters.", false, true, true)
	dataForGraphite, num := getGraphiteSendForCounter(cnt, "logins:1|c\nlogins:2|c\nlogins:3|c")

	assert.Equal(t, num, int64(1))
	assert.Equal(t, "counters.logins.count 6 1\nrates.logins.rate 0.6 1\n", dataForGraphite)
}
예제 #19
0
func TestStats(t *testing.T) {
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)

	options := NewNsqdOptions()
	tcpAddr, _, nsqd := mustStartNSQd(options)
	defer nsqd.Exit()

	topicName := "test_stats" + strconv.Itoa(int(time.Now().Unix()))
	topic := nsqd.GetTopic(topicName)
	msg := nsq.NewMessage(<-nsqd.idChan, []byte("test body"))
	topic.PutMessage(msg)

	conn, err := mustConnectNSQd(tcpAddr)
	assert.Equal(t, err, nil)

	identify(t, conn)
	sub(t, conn, topicName, "ch")

	stats := nsqd.getStats()
	assert.Equal(t, len(stats), 1)
	assert.Equal(t, len(stats[0].Channels), 1)
	assert.Equal(t, len(stats[0].Channels[0].Clients), 1)
	log.Printf("stats: %+v", stats)
}
예제 #20
0
func TestCountersLegacyNamespaceTrueFlushCountsFalse(t *testing.T) {
	cnt := counters.New("stats.", "stats_counts.", true, true, false)
	dataForGraphite, num := getGraphiteSendForCounter(cnt, "logins:1|c\nlogins:2|c\nlogins:3|c")

	assert.Equal(t, num, int64(1))
	assert.Equal(t, "stats.logins 0.6 1\n", dataForGraphite)
}
예제 #21
0
func TestSportsDataTimeConversion(t *testing.T) {
	sd := new(SportsData)
	sd.StartTime = "2011-12-09T07:11:45-05:00"
	startTime := sd.ParsedStartTime()
	assert.Equal(t, 12, startTime.Month)
	assert.Equal(t, 9, startTime.Day)
}
예제 #22
0
func TestCycle(t *testing.T) {
	env := Env{Renderer: new(MockRenderer), Reloader: new(MockReloader)}

	env.Cycle()
	assert.Equal(t, env.Renderer.(*MockRenderer).Called, true)
	assert.Equal(t, env.Reloader.(*MockReloader).Called, true)
}
예제 #23
0
func TestHashCheck(t *testing.T) {
	h, err := x.Hash(x.PwCost, []byte("bar"), salt)
	assert.Equal(t, err, nil)
	ok, err := x.Check(x.PwCost, h, []byte("bar"), salt)
	assert.Equal(t, ok, true)
	assert.Equal(t, err, nil)
}
예제 #24
0
파일: channel_test.go 프로젝트: jsocol/nsq
func TestChannelEmptyConsumer(t *testing.T) {
	log.SetOutput(ioutil.Discard)
	defer log.SetOutput(os.Stdout)

	options := NewNSQDOptions()
	tcpAddr, _, nsqd := mustStartNSQD(options)
	defer nsqd.Exit()
	conn, _ := mustConnectNSQD(tcpAddr)

	topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
	topic := nsqd.GetTopic(topicName)
	channel := topic.GetChannel("channel")
	client := NewClientV2(0, conn, &Context{nsqd})
	client.SetReadyCount(25)
	channel.AddClient(client.ID, client)

	for i := 0; i < 25; i++ {
		msg := nsq.NewMessage(<-nsqd.idChan, []byte("test"))
		channel.StartInFlightTimeout(msg, 0, options.MsgTimeout)
		client.SendingMessage()
	}

	for _, cl := range channel.clients {
		stats := cl.Stats()
		assert.Equal(t, stats.InFlightCount, int64(25))
	}

	channel.Empty()

	for _, cl := range channel.clients {
		stats := cl.Stats()
		assert.Equal(t, stats.InFlightCount, int64(0))
	}

}
예제 #25
0
파일: csv_test.go 프로젝트: gwenn/gosqlite
func TestImportAffinity(t *testing.T) {
	db := open(t)
	defer checkClose(db, t)

	r := strings.NewReader("t,i,r,b,n\n123,123,123,123,123")
	ic := ImportConfig{
		Name:      "test",
		Separator: ',',
		Headers:   true,
		Types:     []Affinity{Textual, Integral, Real, None, Numerical},
		Log:       os.Stderr,
	}
	err := db.ImportCSV(r, ic, "", "test")
	checkNoError(t, err, "error while importing CSV file: %s")
	err = db.Select("SELECT typeof(t), typeof(i), typeof(r), typeof(b), typeof(n) from test", func(s *Stmt) error {
		tot, _ := s.ScanText(0)
		assert.Equal(t, "text", tot)
		toi, _ := s.ScanText(1)
		assert.Equal(t, "integer", toi)
		tor, _ := s.ScanText(2)
		assert.Equal(t, "real", tor)
		tob, _ := s.ScanText(3)
		assert.Equal(t, "text", tob)
		ton, _ := s.ScanText(4)
		assert.Equal(t, "integer", ton)
		return nil
	})
	checkNoError(t, err, "error while selecting: %s")
}
예제 #26
0
파일: apply_test.go 프로젝트: jingweno/gh
func TestTransformApplyArgs(t *testing.T) {
	args := NewArgs([]string{"apply", "https://github.com/jingweno/gh/pull/55"})
	transformApplyArgs(args)

	cmds := args.Commands()
	assert.Equal(t, 2, len(cmds))
	curlString := fmt.Sprintf("curl -#LA %s https://github.com/jingweno/gh/pull/55.patch -o .+55.patch", fmt.Sprintf("gh %s", Version))
	curlRegexp := regexp.MustCompile(curlString)
	applyString := "git apply"
	assert.T(t, curlRegexp.MatchString(cmds[0].String()))
	assert.T(t, strings.Contains(cmds[1].String(), applyString))

	args = NewArgs([]string{"apply", "--ignore-whitespace", "https://github.com/jingweno/gh/commit/fdb9921"})
	transformApplyArgs(args)

	cmds = args.Commands()
	assert.Equal(t, 2, len(cmds))
	curlString = fmt.Sprintf("curl -#LA %s https://github.com/jingweno/gh/commit/fdb9921.patch -o .+fdb9921.patch", fmt.Sprintf("gh %s", Version))
	curlRegexp = regexp.MustCompile(curlString)
	applyString = "git apply --ignore-whitespace"
	assert.T(t, curlRegexp.MatchString(cmds[0].String()))
	assert.T(t, strings.Contains(cmds[1].String(), applyString))

	args = NewArgs([]string{"apply", "https://gist.github.com/8da7fb575debd88c54cf"})
	transformApplyArgs(args)

	cmds = args.Commands()
	assert.Equal(t, 2, len(cmds))
	curlString = fmt.Sprintf("curl -#LA %s https://gist.github.com/8da7fb575debd88c54cf.txt -o .+8da7fb575debd88c54cf.txt", fmt.Sprintf("gh %s", Version))
	curlRegexp = regexp.MustCompile(curlString)
	applyString = "git apply"
	assert.T(t, curlRegexp.MatchString(cmds[0].String()))
	assert.T(t, strings.Contains(cmds[1].String(), applyString))
}
예제 #27
0
func TestProgressWriterIgnoreTotal(t *testing.T) {
	filename := "progress_test.go"
	f, err := os.Open(filename)
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	fs, err := os.Stat(filename)
	if err != nil {
		log.Fatalln(err)
	}

	p := New()
	p.IgnoreTotal = true
	p.Progress = func(current, total, expected int64) {
		log.Println("Ignore total writing", current, total, expected)
		assert.Equal(t, true, current >= total)
	}

	b := new(bytes.Buffer)
	w := io.MultiWriter(p, b)
	_, err = io.Copy(w, f)
	if err != nil {
		log.Fatalln(err)
	}
	assert.Equal(t, fs.Size(), int64(b.Len()))
}
예제 #28
0
func TestCount(t *testing.T) {
	vals := []string{"a", "b", "a", "a", "c", "c"}
	c := NewCounter(vals)
	assert.Equal(t, 3, c["a"])
	assert.Equal(t, 1, c["b"])
	assert.Equal(t, 2, c["c"])
}
예제 #29
0
// Test key consistency
func TestKeyMatch(t *testing.T) {
	tree1 := new(MemPrefixTree)
	tree1.Init()
	for i := 1; i < 100; i++ {
		tree1.Insert(Zi(P_SKS, 65537*i+i))
	}
	// Some extra samples
	for i := 1; i < 50; i++ {
		tree1.Insert(Zi(P_SKS, 68111*i))
	}
	tree2 := new(MemPrefixTree)
	tree2.Init()
	for i := 1; i < 100; i++ {
		tree2.Insert(Zi(P_SKS, 65537*i))
	}
	// One extra sample
	for i := 1; i < 20; i++ {
		tree2.Insert(Zi(P_SKS, 70001*i))
	}
	for i := 1; i < 100; i++ {
		zi := Zi(P_SKS, 65537*i)
		bs := NewZpBitstring(zi)
		node1, err := Find(tree1, zi)
		assert.Equal(t, err, nil)
		node2, err := Find(tree2, zi)
		assert.Equal(t, err, nil)
		t.Logf("node1=%v, node2=%v (%b) full=%v", node1.Key(), node2.Key(), zi.Int64(), bs)
		// If keys are different, one must prefix the other.
		assert.T(t, strings.HasPrefix(node1.Key().String(), node2.Key().String()) ||
			strings.HasPrefix(node2.Key().String(), node1.Key().String()))
	}
}
예제 #30
0
func TestPatRoutingMethodNotAllowed(t *testing.T) {
	p := New()

	var ok bool
	p.Post("/foo/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ok = true
	}))

	p.Put("/foo/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ok = true
	}))

	r, err := http.NewRequest("GET", "/foo/keith", nil)
	if err != nil {
		t.Fatal(err)
	}

	rr := httptest.NewRecorder()
	p.ServeHTTP(rr, r)

	assert.T(t, !ok)
	assert.Equal(t, http.StatusMethodNotAllowed, rr.Code)

	allowed := strings.Split(rr.Header().Get("Allow"), ", ")
	sort.Strings(allowed)
	assert.Equal(t, allowed, []string{"POST", "PUT"})
}