本文整理匯總了Golang中github.com/nsqio/nsq/internal/http_api.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: API
func API(endpoint string) (data *simplejson.Json, err error) {
d := make(map[string]interface{})
err = http_api.NewClient(nil).NegotiateV1(endpoint, &d)
data = simplejson.New()
data.SetPath(nil, d)
return
}
示例2: TestInactiveNodes
func TestInactiveNodes(t *testing.T) {
opts := NewOptions()
opts.Logger = 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, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
equal(t, err, nil)
ci := clusterinfo.New(nil, http_api.NewClient(nil))
producers, _ := ci.GetLookupdProducers(lookupdHTTPAddrs)
equal(t, len(producers), 1)
equal(t, len(producers[0].Topics), 1)
equal(t, producers[0].Topics[0].Topic, topicName)
equal(t, producers[0].Topics[0].Tombstoned, false)
time.Sleep(250 * time.Millisecond)
producers, _ = ci.GetLookupdProducers(lookupdHTTPAddrs)
equal(t, len(producers), 0)
}
示例3: 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))
}
示例4: TestTombstoneRecover
func TestTombstoneRecover(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.TombstoneLifetime = 50 * time.Millisecond
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
defer nsqlookupd.Exit()
topicName := "tombstone_recover"
topicName2 := topicName + "2"
conn := mustConnectLookupd(t, tcpAddr)
defer conn.Close()
identify(t, conn)
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
test.Nil(t, err)
nsq.Register(topicName2, "channel2").WriteTo(conn)
_, err = nsq.ReadResponse(conn)
test.Nil(t, err)
endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s:%d",
httpAddr, topicName, HostAddr, HTTPPort)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(endpoint)
test.Nil(t, err)
pr := ProducersDoc{}
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &pr)
test.Nil(t, err)
test.Equal(t, 0, len(pr.Producers))
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName2)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &pr)
test.Nil(t, err)
test.Equal(t, 1, len(pr.Producers))
time.Sleep(75 * time.Millisecond)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &pr)
test.Nil(t, err)
test.Equal(t, 1, len(pr.Producers))
}
示例5: New
func New(opts *Options) *NSQD {
n := &NSQD{
flag: flagHealthy,
startTime: time.Now(),
topicMap: make(map[string]*Topic),
idChan: make(chan MessageID, 4096),
exitChan: make(chan int),
notifyChan: make(chan interface{}),
optsNotificationChan: make(chan struct{}, 1),
ci: clusterinfo.New(opts.Logger, http_api.NewClient(nil)),
}
n.swapOpts(opts)
if opts.MaxDeflateLevel < 1 || opts.MaxDeflateLevel > 9 {
n.logf("FATAL: --max-deflate-level must be [1,9]")
os.Exit(1)
}
if opts.ID < 0 || opts.ID >= 1024 {
n.logf("FATAL: --worker-id must be [0,1024)")
os.Exit(1)
}
if opts.StatsdPrefix != "" {
_, port, err := net.SplitHostPort(opts.HTTPAddress)
if err != nil {
n.logf("ERROR: failed to parse HTTP address (%s) - %s", opts.HTTPAddress, err)
os.Exit(1)
}
statsdHostKey := statsd.HostKey(net.JoinHostPort(opts.BroadcastAddress, port))
prefixWithHost := strings.Replace(opts.StatsdPrefix, "%s", statsdHostKey, -1)
if prefixWithHost[len(prefixWithHost)-1] != '.' {
prefixWithHost += "."
}
opts.StatsdPrefix = prefixWithHost
}
if opts.TLSClientAuthPolicy != "" && opts.TLSRequired == TLSNotRequired {
opts.TLSRequired = TLSRequired
}
tlsConfig, err := buildTLSConfig(opts)
if err != nil {
n.logf("FATAL: failed to build TLS config - %s", err)
os.Exit(1)
}
if tlsConfig == nil && opts.TLSRequired != TLSNotRequired {
n.logf("FATAL: cannot require TLS client connections without TLS key and cert")
os.Exit(1)
}
n.tlsConfig = tlsConfig
n.logf(version.String("nsqd"))
n.logf("ID: %d", opts.ID)
return n
}
示例6: NewHTTPServer
func NewHTTPServer(ctx *Context) *httpServer {
log := http_api.Log(ctx.nsqadmin.getOpts().Logger)
client := http_api.NewClient(ctx.nsqadmin.httpClientTLSConfig, ctx.nsqadmin.getOpts().HTTPClientConnectTimeout,
ctx.nsqadmin.getOpts().HTTPClientRequestTimeout)
router := httprouter.New()
router.HandleMethodNotAllowed = true
router.PanicHandler = http_api.LogPanicHandler(ctx.nsqadmin.getOpts().Logger)
router.NotFound = http_api.LogNotFoundHandler(ctx.nsqadmin.getOpts().Logger)
router.MethodNotAllowed = http_api.LogMethodNotAllowedHandler(ctx.nsqadmin.getOpts().Logger)
s := &httpServer{
ctx: ctx,
router: router,
client: client,
ci: clusterinfo.New(ctx.nsqadmin.getOpts().Logger, client),
}
router.Handle("GET", "/ping", http_api.Decorate(s.pingHandler, log, http_api.PlainText))
router.Handle("GET", "/", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/topics", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/topics/:topic", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/topics/:topic/:channel", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/nodes", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/nodes/:node", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/counter", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/lookup", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/static/:asset", http_api.Decorate(s.staticAssetHandler, log, http_api.PlainText))
router.Handle("GET", "/fonts/:asset", http_api.Decorate(s.staticAssetHandler, log, http_api.PlainText))
if s.ctx.nsqadmin.getOpts().ProxyGraphite {
proxy := NewSingleHostReverseProxy(ctx.nsqadmin.graphiteURL, ctx.nsqadmin.getOpts().HTTPClientConnectTimeout,
ctx.nsqadmin.getOpts().HTTPClientRequestTimeout)
router.Handler("GET", "/render", proxy)
}
// v1 endpoints
router.Handle("GET", "/api/topics", http_api.Decorate(s.topicsHandler, log, http_api.V1))
router.Handle("GET", "/api/topics/:topic", http_api.Decorate(s.topicHandler, log, http_api.V1))
router.Handle("GET", "/api/topics/:topic/:channel", http_api.Decorate(s.channelHandler, log, http_api.V1))
router.Handle("GET", "/api/nodes", http_api.Decorate(s.nodesHandler, log, http_api.V1))
router.Handle("GET", "/api/nodes/:node", http_api.Decorate(s.nodeHandler, log, http_api.V1))
router.Handle("POST", "/api/topics", http_api.Decorate(s.createTopicChannelHandler, log, http_api.V1))
router.Handle("POST", "/api/topics/:topic", http_api.Decorate(s.topicActionHandler, log, http_api.V1))
router.Handle("POST", "/api/topics/:topic/:channel", http_api.Decorate(s.channelActionHandler, log, http_api.V1))
router.Handle("DELETE", "/api/nodes/:node", http_api.Decorate(s.tombstoneNodeForTopicHandler, log, http_api.V1))
router.Handle("DELETE", "/api/topics/:topic", http_api.Decorate(s.deleteTopicHandler, log, http_api.V1))
router.Handle("DELETE", "/api/topics/:topic/:channel", http_api.Decorate(s.deleteChannelHandler, log, http_api.V1))
router.Handle("GET", "/api/counter", http_api.Decorate(s.counterHandler, log, http_api.V1))
router.Handle("GET", "/api/graphite", http_api.Decorate(s.graphiteHandler, log, http_api.V1))
router.Handle("GET", "/config/:opt", http_api.Decorate(s.doConfig, log, http_api.V1))
router.Handle("PUT", "/config/:opt", http_api.Decorate(s.doConfig, log, http_api.V1))
return s
}
示例7: 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)
}
示例8: TestTombstonedNodes
func TestTombstonedNodes(t *testing.T) {
opts := NewOptions()
opts.Logger = newTestLogger(t)
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, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
equal(t, err, nil)
ci := clusterinfo.New(nil, http_api.NewClient(nil))
producers, _ := ci.GetLookupdProducers(lookupdHTTPAddrs)
equal(t, len(producers), 1)
equal(t, len(producers[0].Topics), 1)
equal(t, producers[0].Topics[0].Topic, topicName)
equal(t, producers[0].Topics[0].Tombstoned, false)
endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s",
httpAddr, topicName, "ip.address:5555")
err = http_api.NewClient(nil).POSTV1(endpoint)
equal(t, err, nil)
producers, _ = ci.GetLookupdProducers(lookupdHTTPAddrs)
equal(t, len(producers), 1)
equal(t, len(producers[0].Topics), 1)
equal(t, producers[0].Topics[0].Topic, topicName)
equal(t, producers[0].Topics[0].Tombstoned, true)
}
示例9: TestTombstonedNodes
func TestTombstonedNodes(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
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)
endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s:%d",
httpAddr, topicName, HostAddr, HTTPPort)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).POSTV1(endpoint)
test.Nil(t, err)
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, true, producers[0].Topics[0].Tombstoned)
}
示例10: TestTombstoneRecover
func TestTombstoneRecover(t *testing.T) {
opts := NewOptions()
opts.Logger = newTestLogger(t)
opts.TombstoneLifetime = 50 * time.Millisecond
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
defer nsqlookupd.Exit()
topicName := "tombstone_recover"
topicName2 := topicName + "2"
conn := mustConnectLookupd(t, tcpAddr)
defer conn.Close()
identify(t, conn, "ip.address", 5000, 5555, "fake-version")
nsq.Register(topicName, "channel1").WriteTo(conn)
_, err := nsq.ReadResponse(conn)
equal(t, err, nil)
nsq.Register(topicName2, "channel2").WriteTo(conn)
_, err = nsq.ReadResponse(conn)
equal(t, err, nil)
endpoint := fmt.Sprintf("http://%s/topic/tombstone?topic=%s&node=%s",
httpAddr, topicName, "ip.address:5555")
err = http_api.NewClient(nil).POSTV1(endpoint)
equal(t, err, nil)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err := API(endpoint)
equal(t, err, nil)
producers, _ := data.Get("producers").Array()
equal(t, len(producers), 0)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName2)
data, err = API(endpoint)
equal(t, err, nil)
producers, _ = data.Get("producers").Array()
equal(t, len(producers), 1)
time.Sleep(75 * time.Millisecond)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
data, err = API(endpoint)
equal(t, err, nil)
producers, _ = data.Get("producers").Array()
equal(t, len(producers), 1)
}
示例11: TestChannelUnregister
func TestChannelUnregister(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
defer nsqlookupd.Exit()
topics := nsqlookupd.DB.FindRegistrations("topic", "*", "*")
test.Equal(t, 0, len(topics))
topicName := "channel_unregister"
conn := mustConnectLookupd(t, tcpAddr)
defer conn.Close()
identify(t, conn)
nsq.Register(topicName, "ch1").WriteTo(conn)
v, err := nsq.ReadResponse(conn)
test.Nil(t, err)
test.Equal(t, []byte("OK"), v)
topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
test.Equal(t, 1, len(topics))
channels := nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
test.Equal(t, 1, len(channels))
nsq.UnRegister(topicName, "ch1").WriteTo(conn)
v, err = nsq.ReadResponse(conn)
test.Nil(t, err)
test.Equal(t, []byte("OK"), v)
topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
test.Equal(t, 1, len(topics))
// we should still have mention of the topic even though there is no producer
// (ie. we haven't *deleted* the channel, just unregistered as a producer)
channels = nsqlookupd.DB.FindRegistrations("channel", topicName, "*")
test.Equal(t, 1, len(channels))
pr := ProducersDoc{}
endpoint := fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &pr)
test.Nil(t, err)
t.Logf("got %v", pr)
test.Equal(t, 1, len(pr.Producers))
}
示例12: QueryAuthd
func QueryAuthd(authd, remoteIP, tlsEnabled, authSecret string,
connectTimeout time.Duration, requestTimeout time.Duration) (*State, error) {
v := url.Values{}
v.Set("remote_ip", remoteIP)
v.Set("tls", tlsEnabled)
v.Set("secret", authSecret)
endpoint := fmt.Sprintf("http://%s/auth?%s", authd, v.Encode())
var authState State
client := http_api.NewClient(nil, connectTimeout, requestTimeout)
if err := client.GETV1(endpoint, &authState); err != nil {
return nil, err
}
// validation on response
for _, auth := range authState.Authorizations {
for _, p := range auth.Permissions {
switch p {
case "subscribe", "publish":
default:
return nil, fmt.Errorf("unknown permission %s", p)
}
}
if _, err := regexp.Compile(auth.Topic); err != nil {
return nil, fmt.Errorf("unable to compile topic %q %s", auth.Topic, err)
}
for _, channel := range auth.Channels {
if _, err := regexp.Compile(channel); err != nil {
return nil, fmt.Errorf("unable to compile channel %q %s", channel, err)
}
}
}
if authState.TTL <= 0 {
return nil, fmt.Errorf("invalid TTL %d (must be >0)", authState.TTL)
}
authState.Expires = time.Now().Add(time.Duration(authState.TTL) * time.Second)
return &authState, nil
}
示例13: syncTopics
func (t *TopicDiscoverer) syncTopics(addrs []string, pattern string) {
newTopics, err := clusterinfo.New(nil, http_api.NewClient(nil)).GetLookupdTopics(addrs)
if err != nil {
log.Printf("ERROR: could not retrieve topic list: %s", err)
}
for _, topic := range newTopics {
if _, ok := t.topics[topic]; !ok {
if !t.allowTopicName(pattern, topic) {
log.Println("Skipping topic ", topic, "as it didn't match required pattern:", pattern)
continue
}
logger, err := newConsumerFileLogger(topic, t.cfg)
if err != nil {
log.Printf("ERROR: couldn't create logger for new topic %s: %s", topic, err)
continue
}
t.topics[topic] = logger
go t.startTopicRouter(logger)
}
}
}
示例14: TestCluster
func TestCluster(t *testing.T) {
lopts := nsqlookupd.NewOptions()
lopts.Logger = newTestLogger(t)
lopts.BroadcastAddress = "127.0.0.1"
_, _, lookupd := mustStartNSQLookupd(lopts)
opts := NewOptions()
opts.Logger = newTestLogger(t)
opts.NSQLookupdTCPAddresses = []string{lookupd.RealTCPAddr().String()}
opts.BroadcastAddress = "127.0.0.1"
_, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "cluster_test" + strconv.Itoa(int(time.Now().Unix()))
hostname, err := os.Hostname()
equal(t, err, nil)
url := fmt.Sprintf("http://%s/topic/create?topic=%s", nsqd.RealHTTPAddr(), topicName)
err = http_api.NewClient(nil).POSTV1(url)
equal(t, err, nil)
url = fmt.Sprintf("http://%s/channel/create?topic=%s&channel=ch", nsqd.RealHTTPAddr(), topicName)
err = http_api.NewClient(nil).POSTV1(url)
equal(t, err, nil)
// allow some time for nsqd to push info to nsqlookupd
time.Sleep(350 * time.Millisecond)
endpoint := fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr())
data, err := API(endpoint)
equal(t, err, nil)
topicData := data.Get("topic:" + topicName + ":")
producers, _ := topicData.Array()
equal(t, len(producers), 1)
producer := topicData.GetIndex(0)
equal(t, producer.Get("hostname").MustString(), hostname)
equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1")
equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port)
equal(t, producer.Get("tombstoned").MustBool(), false)
channelData := data.Get("channel:" + topicName + ":ch")
producers, _ = channelData.Array()
equal(t, len(producers), 1)
producer = topicData.GetIndex(0)
equal(t, producer.Get("hostname").MustString(), hostname)
equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1")
equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port)
equal(t, producer.Get("tombstoned").MustBool(), false)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName)
data, err = API(endpoint)
producers, _ = data.Get("producers").Array()
equal(t, len(producers), 1)
producer = data.Get("producers").GetIndex(0)
equal(t, producer.Get("hostname").MustString(), hostname)
equal(t, producer.Get("broadcast_address").MustString(), "127.0.0.1")
equal(t, producer.Get("tcp_port").MustInt(), nsqd.RealTCPAddr().Port)
channels, _ := data.Get("channels").Array()
equal(t, len(channels), 1)
channel := channels[0].(string)
equal(t, channel, "ch")
url = fmt.Sprintf("http://%s/topic/delete?topic=%s", nsqd.RealHTTPAddr(), topicName)
err = http_api.NewClient(nil).POSTV1(url)
equal(t, err, nil)
// allow some time for nsqd to push info to nsqlookupd
time.Sleep(350 * time.Millisecond)
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", lookupd.RealHTTPAddr(), topicName)
data, err = API(endpoint)
equal(t, err, nil)
producers, _ = data.Get("producers").Array()
equal(t, len(producers), 0)
endpoint = fmt.Sprintf("http://%s/debug", lookupd.RealHTTPAddr())
data, err = API(endpoint)
equal(t, err, nil)
producers, _ = data.Get("topic:" + topicName + ":").Array()
equal(t, len(producers), 0)
producers, _ = data.Get("channel:" + topicName + ":ch").Array()
equal(t, len(producers), 0)
}
示例15: TestBasicLookupd
func TestBasicLookupd(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
tcpAddr, httpAddr, nsqlookupd := mustStartLookupd(opts)
defer nsqlookupd.Exit()
topics := nsqlookupd.DB.FindRegistrations("topic", "*", "*")
test.Equal(t, 0, len(topics))
topicName := "connectmsg"
conn := mustConnectLookupd(t, tcpAddr)
identify(t, conn)
nsq.Register(topicName, "channel1").WriteTo(conn)
v, err := nsq.ReadResponse(conn)
test.Nil(t, err)
test.Equal(t, []byte("OK"), v)
pr := ProducersDoc{}
endpoint := fmt.Sprintf("http://%s/nodes", httpAddr)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &pr)
test.Nil(t, err)
t.Logf("got %v", pr)
test.Equal(t, 1, len(pr.Producers))
topics = nsqlookupd.DB.FindRegistrations("topic", topicName, "")
test.Equal(t, 1, len(topics))
producers := nsqlookupd.DB.FindProducers("topic", topicName, "")
test.Equal(t, 1, len(producers))
producer := producers[0]
test.Equal(t, HostAddr, producer.peerInfo.BroadcastAddress)
test.Equal(t, HostAddr, producer.peerInfo.Hostname)
test.Equal(t, TCPPort, producer.peerInfo.TCPPort)
test.Equal(t, HTTPPort, producer.peerInfo.HTTPPort)
tr := TopicsDoc{}
endpoint = fmt.Sprintf("http://%s/topics", httpAddr)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &tr)
test.Nil(t, err)
t.Logf("got %v", tr)
test.Equal(t, 1, len(tr.Topics))
lr := LookupDoc{}
endpoint = fmt.Sprintf("http://%s/lookup?topic=%s", httpAddr, topicName)
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &lr)
test.Nil(t, err)
t.Logf("got %v", lr)
test.Equal(t, 1, len(lr.Channels))
test.Equal(t, 1, len(lr.Producers))
for _, p := range lr.Producers {
test.Equal(t, TCPPort, p.TCPPort)
test.Equal(t, HTTPPort, p.HTTPPort)
test.Equal(t, HostAddr, p.BroadcastAddress)
test.Equal(t, NSQDVersion, p.Version)
}
conn.Close()
time.Sleep(10 * time.Millisecond)
// now there should be no producers, but still topic/channel entries
err = http_api.NewClient(nil, ConnectTimeout, RequestTimeout).NegotiateV1(endpoint, &lr)
test.Nil(t, err)
test.Equal(t, 1, len(lr.Channels))
test.Equal(t, 0, len(lr.Producers))
}