本文整理匯總了Golang中github.com/nsqio/nsq/internal/test.Nil函數的典型用法代碼示例。如果您正苦於以下問題:Golang Nil函數的具體用法?Golang Nil怎麽用?Golang Nil使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Nil函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestHTTPTopicGET
func TestHTTPTopicGET(t *testing.T) {
dataPath, nsqds, nsqlookupds, nsqadmin1 := bootstrapNSQCluster(t)
defer os.RemoveAll(dataPath)
defer nsqds[0].Exit()
defer nsqlookupds[0].Exit()
defer nsqadmin1.Exit()
topicName := "test_topic_get" + strconv.Itoa(int(time.Now().Unix()))
nsqds[0].GetTopic(topicName)
time.Sleep(100 * time.Millisecond)
client := http.Client{}
url := fmt.Sprintf("http://%s/api/topics/%s", nsqadmin1.RealHTTPAddr(), topicName)
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
t.Logf("%s", body)
ts := TopicStatsDoc{}
err = json.Unmarshal(body, &ts)
test.Nil(t, err)
test.Equal(t, topicName, ts.TopicName)
test.Equal(t, 0, int(ts.Depth))
test.Equal(t, 0, int(ts.MemoryDepth))
test.Equal(t, 0, int(ts.BackendDepth))
test.Equal(t, 0, int(ts.MessageCount))
test.Equal(t, false, ts.Paused)
}
示例2: TestHTTPconfig
func TestHTTPconfig(t *testing.T) {
dataPath, nsqds, nsqlookupds, nsqadmin1 := bootstrapNSQCluster(t)
defer os.RemoveAll(dataPath)
defer nsqds[0].Exit()
defer nsqlookupds[0].Exit()
defer nsqadmin1.Exit()
lopts := nsqlookupd.NewOptions()
lopts.Logger = test.NewTestLogger(t)
_, _, lookupd1 := mustStartNSQLookupd(lopts)
defer lookupd1.Exit()
_, _, lookupd2 := mustStartNSQLookupd(lopts)
defer lookupd2.Exit()
url := fmt.Sprintf("http://%s/config/nsqlookupd_http_addresses", nsqadmin1.RealHTTPAddr())
resp, err := http.Get(url)
test.Nil(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
origaddrs := fmt.Sprintf(`["%s"]`, nsqlookupds[0].RealHTTPAddr().String())
test.Equal(t, origaddrs, string(body))
client := http.Client{}
addrs := fmt.Sprintf(`["%s","%s"]`, lookupd1.RealHTTPAddr().String(), lookupd2.RealHTTPAddr().String())
url = fmt.Sprintf("http://%s/config/nsqlookupd_http_addresses", nsqadmin1.RealHTTPAddr())
req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(addrs)))
test.Nil(t, err)
resp, err = client.Do(req)
test.Nil(t, err)
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
test.Equal(t, addrs, string(body))
}
示例3: TestHTTPPauseChannelPOST
func TestHTTPPauseChannelPOST(t *testing.T) {
dataPath, nsqds, nsqlookupds, nsqadmin1 := bootstrapNSQCluster(t)
defer os.RemoveAll(dataPath)
defer nsqds[0].Exit()
defer nsqlookupds[0].Exit()
defer nsqadmin1.Exit()
topicName := "test_pause_channel_post" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqds[0].GetTopic(topicName)
topic.GetChannel("ch")
time.Sleep(100 * time.Millisecond)
client := http.Client{}
url := fmt.Sprintf("http://%s/api/topics/%s/ch", nsqadmin1.RealHTTPAddr(), topicName)
body, _ := json.Marshal(map[string]interface{}{
"action": "pause",
})
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
resp, err := client.Do(req)
test.Nil(t, err)
body, _ = ioutil.ReadAll(resp.Body)
test.Equal(t, 200, resp.StatusCode)
resp.Body.Close()
url = fmt.Sprintf("http://%s/api/topics/%s/ch", nsqadmin1.RealHTTPAddr(), topicName)
body, _ = json.Marshal(map[string]interface{}{
"action": "unpause",
})
req, _ = http.NewRequest("POST", url, bytes.NewBuffer(body))
resp, err = client.Do(req)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
resp.Body.Close()
}
示例4: TestHTTPmputEmpty
func TestHTTPmputEmpty(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_http_mput_empty" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := []byte("test message")
msgs := make([][]byte, 4)
for i := range msgs {
msgs[i] = msg
}
buf := bytes.NewBuffer(bytes.Join(msgs, []byte("\n")))
_, err := buf.Write([]byte("\n"))
test.Nil(t, err)
url := fmt.Sprintf("http://%s/mput?topic=%s", httpAddr, topicName)
resp, err := http.Post(url, "application/octet-stream", buf)
test.Nil(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
test.Equal(t, "OK", string(body))
time.Sleep(5 * time.Millisecond)
test.Equal(t, int64(4), topic.Depth())
}
示例5: TestBasicV2
// exercise the basic operations of the V2 protocol
func TestBasicV2(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.ClientTimeout = 60 * time.Second
tcpAddr, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_v2" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := NewMessage(<-nsqd.idChan, []byte("test body"))
topic.PutMessage(msg)
conn, err := mustConnectNSQD(tcpAddr)
test.Nil(t, err)
defer conn.Close()
identify(t, conn, nil, frameTypeResponse)
sub(t, conn, topicName, "ch")
_, err = nsq.Ready(1).WriteTo(conn)
test.Nil(t, err)
resp, err := nsq.ReadResponse(conn)
test.Nil(t, err)
frameType, data, err := nsq.UnpackResponse(resp)
msgOut, _ := decodeMessage(data)
test.Equal(t, frameTypeMessage, frameType)
test.Equal(t, msg.ID, msgOut.ID)
test.Equal(t, msg.Body, msgOut.Body)
test.Equal(t, uint16(1), msgOut.Attempts)
}
示例6: TestHTTPNodesGET
func TestHTTPNodesGET(t *testing.T) {
dataPath, nsqds, nsqlookupds, nsqadmin1 := bootstrapNSQCluster(t)
defer os.RemoveAll(dataPath)
defer nsqds[0].Exit()
defer nsqlookupds[0].Exit()
defer nsqadmin1.Exit()
time.Sleep(100 * time.Millisecond)
client := http.Client{}
url := fmt.Sprintf("http://%s/api/nodes", nsqadmin1.RealHTTPAddr())
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
hostname, _ := os.Hostname()
t.Logf("%s", body)
ns := NodesDoc{}
err = json.Unmarshal(body, &ns)
test.Nil(t, err)
test.Equal(t, 1, len(ns.Nodes))
testNode := ns.Nodes[0]
test.Equal(t, hostname, testNode.Hostname)
test.Equal(t, "127.0.0.1", testNode.BroadcastAddress)
test.Equal(t, nsqds[0].RealTCPAddr().Port, testNode.TCPPort)
test.Equal(t, nsqds[0].RealHTTPAddr().Port, testNode.HTTPPort)
test.Equal(t, version.Binary, testNode.Version)
test.Equal(t, 0, len(testNode.Topics))
}
示例7: readValidate
func readValidate(t *testing.T, conn io.Reader, f int32, d string) []byte {
resp, err := nsq.ReadResponse(conn)
test.Nil(t, err)
frameType, data, err := nsq.UnpackResponse(resp)
test.Nil(t, err)
test.Equal(t, f, frameType)
test.Equal(t, d, string(data))
return data
}
示例8: TestDeleteTopic
func TestDeleteTopic(t *testing.T) {
dataPath, nsqds, nsqlookupd1 := bootstrapNSQCluster(t)
defer os.RemoveAll(dataPath)
defer nsqds[0].Exit()
defer nsqlookupd1.Exit()
em := ErrMessage{}
client := http.Client{}
url := fmt.Sprintf("http://%s/topic/delete", nsqlookupd1.RealHTTPAddr())
req, _ := http.NewRequest("POST", url, nil)
resp, err := client.Do(req)
test.Nil(t, err)
test.Equal(t, 400, resp.StatusCode)
test.Equal(t, "Bad Request", http.StatusText(resp.StatusCode))
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
t.Logf("%s", body)
err = json.Unmarshal(body, &em)
test.Nil(t, err)
test.Equal(t, "MISSING_ARG_TOPIC", em.Message)
topicName := "sampletopicA" + strconv.Itoa(int(time.Now().Unix()))
makeTopic(nsqlookupd1, topicName)
url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
req, _ = http.NewRequest("POST", url, nil)
resp, err = client.Do(req)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
t.Logf("%s", body)
test.Equal(t, []byte(""), body)
topicName = "sampletopicB" + strconv.Itoa(int(time.Now().Unix()))
channelName := "foobar" + strconv.Itoa(int(time.Now().Unix()))
makeChannel(nsqlookupd1, topicName, channelName)
url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqlookupd1.RealHTTPAddr(), topicName)
req, _ = http.NewRequest("POST", url, nil)
resp, err = client.Do(req)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
t.Logf("%s", body)
test.Equal(t, []byte(""), body)
}
示例9: TestClientAttributes
func TestClientAttributes(t *testing.T) {
userAgent := "Test User Agent"
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.Verbose = true
opts.SnappyEnabled = true
tcpAddr, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
conn, err := mustConnectNSQD(tcpAddr)
test.Nil(t, err)
defer conn.Close()
data := identify(t, conn, map[string]interface{}{
"snappy": true,
"user_agent": userAgent,
}, frameTypeResponse)
resp := struct {
Snappy bool `json:"snappy"`
UserAgent string `json:"user_agent"`
}{}
err = json.Unmarshal(data, &resp)
test.Nil(t, err)
test.Equal(t, true, resp.Snappy)
r := snappystream.NewReader(conn, snappystream.SkipVerifyChecksum)
w := snappystream.NewWriter(conn)
readValidate(t, r, frameTypeResponse, "OK")
topicName := "test_client_attributes" + strconv.Itoa(int(time.Now().Unix()))
sub(t, readWriter{r, w}, topicName, "ch")
var d struct {
Topics []struct {
Channels []struct {
Clients []struct {
UserAgent string `json:"user_agent"`
Snappy bool `json:"snappy"`
} `json:"clients"`
} `json:"channels"`
} `json:"topics"`
}
endpoint := fmt.Sprintf("http://127.0.0.1:%d/stats?format=json", httpAddr.Port)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).GETV1(endpoint, &d)
test.Nil(t, err)
test.Equal(t, userAgent, d.Topics[0].Channels[0].Clients[0].UserAgent)
test.Equal(t, true, d.Topics[0].Channels[0].Clients[0].Snappy)
}
示例10: identify
func identify(t *testing.T, conn net.Conn) {
ci := make(map[string]interface{})
ci["tcp_port"] = TCPPort
ci["http_port"] = HTTPPort
ci["broadcast_address"] = HostAddr
ci["hostname"] = HostAddr
ci["version"] = NSQDVersion
cmd, _ := nsq.Identify(ci)
_, err := cmd.WriteTo(conn)
test.Nil(t, err)
_, err = nsq.ReadResponse(conn)
test.Nil(t, err)
}
示例11: TestMultipleConsumerV2
func TestMultipleConsumerV2(t *testing.T) {
msgChan := make(chan *Message)
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.ClientTimeout = 60 * time.Second
tcpAddr, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_multiple_v2" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
msg := NewMessage(<-nsqd.idChan, []byte("test body"))
topic.GetChannel("ch1")
topic.GetChannel("ch2")
topic.PutMessage(msg)
for _, i := range []string{"1", "2"} {
conn, err := mustConnectNSQD(tcpAddr)
test.Nil(t, err)
defer conn.Close()
identify(t, conn, nil, frameTypeResponse)
sub(t, conn, topicName, "ch"+i)
_, err = nsq.Ready(1).WriteTo(conn)
test.Nil(t, err)
go func(c net.Conn) {
resp, err := nsq.ReadResponse(c)
test.Nil(t, err)
_, data, err := nsq.UnpackResponse(resp)
test.Nil(t, err)
msg, err := decodeMessage(data)
test.Nil(t, err)
msgChan <- msg
}(conn)
}
msgOut := <-msgChan
test.Equal(t, msg.ID, msgOut.ID)
test.Equal(t, msg.Body, msgOut.Body)
test.Equal(t, uint16(1), msgOut.Attempts)
msgOut = <-msgChan
test.Equal(t, msg.ID, msgOut.ID)
test.Equal(t, msg.Body, msgOut.Body)
test.Equal(t, uint16(1), msgOut.Attempts)
}
示例12: TestChannelHealth
func TestChannelHealth(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.MemQueueSize = 2
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topic := nsqd.GetTopic("test")
channel := topic.GetChannel("channel")
channel.backend = &errorBackendQueue{}
msg := NewMessage(<-nsqd.idChan, make([]byte, 100))
err := channel.PutMessage(msg)
test.Nil(t, err)
msg = NewMessage(<-nsqd.idChan, make([]byte, 100))
err = channel.PutMessage(msg)
test.Nil(t, err)
msg = NewMessage(<-nsqd.idChan, make([]byte, 100))
err = channel.PutMessage(msg)
test.NotNil(t, err)
url := fmt.Sprintf("http://%s/ping", httpAddr)
resp, err := http.Get(url)
test.Nil(t, err)
test.Equal(t, 500, resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
test.Equal(t, "NOK - never gonna happen", string(body))
channel.backend = &errorRecoveredBackendQueue{}
msg = NewMessage(<-nsqd.idChan, make([]byte, 100))
err = channel.PutMessage(msg)
test.Nil(t, err)
resp, err = http.Get(url)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
test.Equal(t, "OK", string(body))
}
示例13: TestInactiveNodes
func TestInactiveNodes(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.InactiveProducerTimeout = 200 * time.Millisecond
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
defer nsqlookupd.Exit()
lookupdHTTPAddrs := []string{fmt.Sprintf("%s", httpAddr)}
topicName := "inactive_nodes"
conn := mustConnectLookupd(t, tcpAddr)
defer conn.Close()
identify(t, conn)
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
test.Nil(t, err)
ci := clusterinfo.New(nil, http_api.NewClient(nil, ConnectTimeout, RequestTimeout))
producers, _ := ci.GetLookupdProducers(lookupdHTTPAddrs)
test.Equal(t, 1, len(producers))
test.Equal(t, 1, len(producers[0].Topics))
test.Equal(t, topicName, producers[0].Topics[0].Topic)
test.Equal(t, false, producers[0].Topics[0].Tombstoned)
time.Sleep(250 * time.Millisecond)
producers, _ = ci.GetLookupdProducers(lookupdHTTPAddrs)
test.Equal(t, 0, len(producers))
}
示例14: TestHTTPmputBinary
func TestHTTPmputBinary(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_http_mput_bin" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
mpub := make([][]byte, 5)
for i := range mpub {
mpub[i] = make([]byte, 100)
}
cmd, _ := nsq.MultiPublish(topicName, mpub)
buf := bytes.NewBuffer(cmd.Body)
url := fmt.Sprintf("http://%s/mput?topic=%s&binary=true", httpAddr, topicName)
resp, err := http.Post(url, "application/octet-stream", buf)
test.Nil(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
test.Equal(t, "OK", string(body))
time.Sleep(5 * time.Millisecond)
test.Equal(t, int64(5), topic.Depth())
}
示例15: TestTLSRequireVerifyExceptHTTP
func TestTLSRequireVerifyExceptHTTP(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.Verbose = true
opts.TLSCert = "./test/certs/server.pem"
opts.TLSKey = "./test/certs/server.key"
opts.TLSRootCAFile = "./test/certs/ca.pem"
opts.TLSClientAuthPolicy = "require-verify"
opts.TLSRequired = TLSRequiredExceptHTTP
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_http_req_verf_except_http" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
// no cert
buf := bytes.NewBuffer([]byte("test message"))
url := fmt.Sprintf("http://%s/put?topic=%s", httpAddr, topicName)
resp, err := http.Post(url, "application/octet-stream", buf)
test.Nil(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
test.Equal(t, "OK", string(body))
time.Sleep(5 * time.Millisecond)
test.Equal(t, int64(1), topic.Depth())
}