Exemple #1
0
func TestNSQDStatsFilter(t *testing.T) {

	t.Logf("Starts nsqd...")
	nsqdOpts := nsqd.NewOptions()

	nsqdOpts.Logger = newTestLogger(t)
	tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
	if err != nil {
		panic(err)
	}
	nsqdOpts.DataPath = tmpDir

	_, nsqdHTTPAddr, nsqd, _ := mustStartNSQD(nsqdOpts)
	t.Logf("nsqd started")

	//delete existing topics if there is any
	topicCnt := 0
	for _, topic := range nsqd.GetStats(false) {
		parNum, _ := strconv.Atoi(topic.TopicPartition)
		nsqd.DeleteExistingTopic(topic.TopicName, parNum)
		topicCnt++
	}
	t.Logf("%d topic(s) deleted.", topicCnt)
	topicName := fmt.Sprintf("test_nsqd_stats_filter%d", time.Now().UnixNano())
	nsqd.GetTopic(topicName, 0)
	defer nsqd.DeleteExistingTopic(topicName, 0)

	topicNameAnother := fmt.Sprintf("test_nsqd_stats_filter_another%d", time.Now().UnixNano())
	nsqd.GetTopic(topicNameAnother, 0)
	topic_another, err := nsqd.GetExistingTopic(topicNameAnother, 0)
	topic_another.DisableForSlave()
	defer nsqd.DeleteExistingTopic(topicNameAnother, 0)

	time.Sleep(500 * time.Millisecond)

	client := http.Client{}
	url := fmt.Sprintf("http://%s/stats?format=json", nsqdHTTPAddr)
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Add("Accept", "application/vnd.nsq; version=1.0")
	resp, err := client.Do(req)
	test.Assert(t, err == nil, "error in first response.")
	body, _ := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	type NSQDStats struct {
		TopicStats []map[string]interface{} `json:"topics"`
		Version    string                   `json:"version"`
		Health     string                   `json:"health"`
		StartTime  int64                    `json:"start_time"`
	}

	var stats NSQDStats
	json.Unmarshal(body, &stats)
	t.Logf("topic len in first response: %d", len(stats.TopicStats))
	test.Assert(t, len(stats.TopicStats) == 2, "topic number does not match.")

	url = fmt.Sprintf("http://%s/stats?format=json&leaderOnly=true", nsqdHTTPAddr)
	req, _ = http.NewRequest("GET", url, nil)
	req.Header.Add("Accept", "application/vnd.nsq; version=1.0")
	resp, err = client.Do(req)
	test.Assert(t, err == nil, "error in second response.")
	body, _ = ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	var statFiltered NSQDStats
	json.Unmarshal(body, &statFiltered)
	t.Logf("topic len: %d", len(statFiltered.TopicStats))
	test.Assert(t, len(statFiltered.TopicStats) == 1, "topic number is not empty.")
}
Exemple #2
0
func TestHTTPV1TopicChannel(t *testing.T) {
	opts := nsqd.NewOptions()
	opts.Logger = newTestLogger(t)
	_, httpAddr, nsqd, nsqdServer := mustStartNSQD(opts)
	defer os.RemoveAll(opts.DataPath)
	defer nsqdServer.Exit()

	topicName := "test_http_topic_channel2" + strconv.Itoa(int(time.Now().Unix()))
	topicPart := 0
	channelName := "ch2"

	nsqd.GetTopicIgnPart(topicName)

	url := fmt.Sprintf("http://%s/channel/create?topic=%s&channel=%s", httpAddr, topicName, channelName)
	resp, err := http.Post(url, "application/json", nil)
	test.Equal(t, err, nil)
	test.Equal(t, resp.StatusCode, 200)
	body, _ := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	test.Equal(t, string(body), "")
	test.Equal(t, resp.Header.Get("X-NSQ-Content-Type"), "nsq; version=1.0")

	topic, err := nsqd.GetExistingTopic(topicName, topicPart)
	test.Equal(t, err, nil)
	test.NotNil(t, topic)

	channel, err := topic.GetExistingChannel(channelName)
	test.Equal(t, err, nil)
	test.NotNil(t, channel)

	url = fmt.Sprintf("http://%s/channel/pause?topic=%s&channel=%s", httpAddr, topicName, channelName)
	resp, err = http.Post(url, "application/json", nil)
	test.Equal(t, err, nil)
	test.Equal(t, resp.StatusCode, 200)
	body, _ = ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	test.Equal(t, string(body), "")
	test.Equal(t, resp.Header.Get("X-NSQ-Content-Type"), "nsq; version=1.0")

	test.Equal(t, channel.IsPaused(), true)

	url = fmt.Sprintf("http://%s/channel/unpause?topic=%s&channel=%s", httpAddr, topicName, channelName)
	resp, err = http.Post(url, "application/json", nil)
	test.Equal(t, err, nil)
	test.Equal(t, resp.StatusCode, 200)
	body, _ = ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	test.Equal(t, string(body), "")
	test.Equal(t, resp.Header.Get("X-NSQ-Content-Type"), "nsq; version=1.0")

	test.Equal(t, channel.IsPaused(), false)

	url = fmt.Sprintf("http://%s/channel/delete?topic=%s&channel=%s", httpAddr, topicName, channelName)
	resp, err = http.Post(url, "application/json", nil)
	test.Equal(t, err, nil)
	test.Equal(t, resp.StatusCode, 200)
	body, _ = ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	test.Equal(t, string(body), "")
	test.Equal(t, resp.Header.Get("X-NSQ-Content-Type"), "nsq; version=1.0")

	_, err = topic.GetExistingChannel(channelName)
	test.NotNil(t, err)

	nsqd.DeleteExistingTopic(topicName, topicPart)
	_, err = nsqd.GetExistingTopic(topicName, topicPart)
	test.NotNil(t, err)
}