本文整理匯總了Golang中github.com/mozilla-services/pushgo/id.Generate函數的典型用法代碼示例。如果您正苦於以下問題:Golang Generate函數的具體用法?Golang Generate怎麽用?Golang Generate使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Generate函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestHelo
func (t caseTest) TestHelo() error {
deviceId, err := id.Generate()
if err != nil {
return fmt.Errorf("On test %v, error generating device ID: %#v", t.CaseTestType, err)
}
addExistsHook(deviceId, true)
defer removeExistsHook(deviceId)
channelId, err := id.Generate()
if err != nil {
return fmt.Errorf("On test %v, error generating channel ID: %#v", t.CaseTestType, err)
}
origin, err := testServer.Origin()
if err != nil {
return fmt.Errorf("On test %v, error initializing test server: %#v", t.CaseTestType, err)
}
conn, err := client.DialOrigin(origin)
if err != nil {
return fmt.Errorf("On test %v, error dialing origin: %#v", t.CaseTestType, err)
}
defer conn.Close()
defer conn.Purge()
request := CaseHelo{t.CaseTestType, client.NewHelo(deviceId, []string{channelId}).(client.ClientHelo)}
reply, err := conn.WriteRequest(request)
if t.statusCode >= 200 && t.statusCode < 300 {
if err != nil {
return fmt.Errorf("On test %v, error writing handshake request: %#v", t.CaseTestType, err)
}
helo, ok := reply.(client.ServerHelo)
if !ok {
return fmt.Errorf("On test %v, type assertion failed for handshake reply: %#v", t.CaseTestType, reply)
}
if helo.StatusCode != t.statusCode {
return fmt.Errorf("On test %v, unexpected reply status: got %#v; want %#v", t.CaseTestType, helo.StatusCode, t.statusCode)
}
if t.shouldReset {
if helo.DeviceId == deviceId {
return fmt.Errorf("On test %v, want new device ID; got %#v", t.CaseTestType, deviceId)
}
return nil
}
if helo.DeviceId != deviceId {
return fmt.Errorf("On test %v, mismatched device ID: got %#v; want %#v", t.CaseTestType, helo.DeviceId, deviceId)
}
return nil
}
if err != io.EOF {
return fmt.Errorf("On test %v, error writing handshake: got %#v; want io.EOF", t.CaseTestType, err)
}
err = conn.Close()
clientErr, ok := err.(client.Error)
if !ok {
return fmt.Errorf("On test %v, type assertion failed for close error: %#v", t.CaseTestType, err)
}
if clientErr.Status() != t.statusCode {
return fmt.Errorf("On test %v, unexpected close error status: got %#v; want %#v", t.CaseTestType, clientErr.Status(), t.statusCode)
}
return nil
}
示例2: reconnect
func reconnect(origin, deviceId, channelId, endpoint string) (err error) {
socket, err := ws.Dial(origin, "", origin)
if err != nil {
return fmt.Errorf("Error dialing origin: %s", err)
}
connId, err := id.Generate()
if err != nil {
return fmt.Errorf("Error generating connection ID: %#v", err)
}
conn := client.NewConn(socket, connId, true)
defer conn.Close()
defer conn.Purge()
actualId, err := conn.WriteHelo(deviceId, channelId)
if err != nil {
return fmt.Errorf("Error writing handshake request: %s", err)
}
if actualId != deviceId {
return fmt.Errorf("Mismatched device IDs: got %q; want %q",
actualId, deviceId)
}
if err = roundTrip(conn, deviceId, channelId, endpoint, 2); err != nil {
return fmt.Errorf("Error sending notification after reconnect: %s", err)
}
return nil
}
示例3: TestPrematureUnregister
func TestPrematureUnregister(t *testing.T) {
channelId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating channel ID: %#v", err)
}
origin, err := testServer.Origin()
if err != nil {
t.Fatalf("Error initializing test server: %#v", err)
}
conn, err := client.DialOrigin(origin)
if err != nil {
t.Fatalf("Error dialing origin: %#v", err)
}
defer conn.Close()
defer conn.Purge()
conn.RegisterDecoder("unregister", client.DecoderFunc(decodeUnregisterReply))
request := client.NewUnregister(channelId, true)
_, err = conn.WriteRequest(request)
if err != io.EOF {
t.Fatalf("Error writing deregistration request: got %#v; want io.EOF", err)
}
err = conn.Close()
clientErr, ok := err.(client.Error)
if !ok {
t.Fatalf("Type assertion failed for close error: %#v", err)
}
if clientErr.Status() != 401 {
t.Errorf("Unexpected close error status: got %#v; want 401", clientErr.Status())
}
}
示例4: TestMultipleRegister
func TestMultipleRegister(t *testing.T) {
channelId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating channel ID: %#v", err)
}
origin, err := testServer.Origin()
if err != nil {
t.Fatalf("Error initializing test server: %#v", err)
}
conn, _, err := client.Dial(origin)
if err != nil {
t.Fatalf("Error dialing origin: %#v", err)
}
defer conn.Close()
defer conn.Purge()
request := MultipleRegister{client.NewRegister(channelId).(client.ClientRegister)}
_, err = conn.WriteRequest(request)
if err != io.EOF {
t.Fatalf("Error writing registration request: got %#v; want io.EOF", err)
}
err = conn.Close()
clientErr, ok := err.(client.Error)
if !ok {
t.Fatalf("Type assertion failed for close error: %#v", err)
}
if clientErr.Status() != 401 {
t.Errorf("Unexpected close error status: got %#v; want 401", clientErr.Status())
}
}
示例5: Status
// Status determines whether etcd can respond to requests. Implements
// Locator.Status().
func (l *EtcdLocator) Status() (ok bool, err error) {
fakeID, err := id.Generate()
if err != nil {
return false, err
}
key, expected := "status_"+fakeID, "test"
if _, err = l.client.Set(key, expected, uint64(6*time.Second)); err != nil {
if l.logger.ShouldLog(ERROR) {
l.logger.Error("etcd", "Error storing health check key",
LogFields{"error": err.Error(), "key": key})
}
return false, err
}
resp, err := l.client.Get(key, false, false)
if err != nil {
if l.logger.ShouldLog(ERROR) {
l.logger.Error("etcd", "Error fetching health check key",
LogFields{"error": err.Error(), "key": key})
}
return false, err
}
if resp.Node.Value != expected {
if l.logger.ShouldLog(ERROR) {
l.logger.Error("etcd", "Unexpected health check result", LogFields{
"key": key, "expected": expected, "actual": resp.Node.Value})
}
return false, ErrEtcdStatus
}
l.client.Delete(key, false)
return true, nil
}
示例6: TestMultiRegister
func TestMultiRegister(t *testing.T) {
channelId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating channel ID: %#v", err)
}
origin, err := testServer.Origin()
if err != nil {
t.Fatalf("Error initializing test server: %#v", err)
}
conn, _, err := client.Dial(origin)
if err != nil {
t.Fatalf("Error dialing origin: %#v", err)
}
defer conn.Close()
defer conn.Purge()
endpoint, err := conn.Register(channelId)
if err != nil {
t.Fatalf("Error writing registration request: %#v", err)
}
if !isValidEndpoint(endpoint) {
t.Errorf("Invalid push endpoint: %#v", endpoint)
}
_, err = conn.Register("")
if err != io.EOF {
t.Fatalf("Error writing malformed registration request: got %#v; want io.EOF", err)
}
err = conn.Close()
if clientErr, ok := err.(client.Error); ok && clientErr.Status() != 401 {
t.Errorf("Unexpected close error status: got %#v; want 401", clientErr.Status())
} else if !ok {
t.Fatalf("Type assertion failed for close error: %#v", err)
}
}
示例7: Dial
func Dial(origin string) (conn *Conn, deviceId string, err error) {
if deviceId, err = id.Generate(); err != nil {
return nil, "", err
}
if conn, err = DialId(origin, &deviceId); err != nil {
return nil, "", err
}
return conn, deviceId, nil
}
示例8: TestHelo
func (t idTest) TestHelo() error {
deviceId, err := id.Generate()
if err != nil {
return fmt.Errorf("On handshake test %v, error generating device ID: %#v", t.name, err)
}
addExistsHook(deviceId, true)
defer removeExistsHook(deviceId)
origin, err := testServer.Origin()
if err != nil {
return fmt.Errorf("On handshake test %v, error initializing test server: %#v", t.name, err)
}
conn, err := client.DialOrigin(origin)
if err != nil {
return fmt.Errorf("On handshake test %v, error dialing origin: %#v", t.name, err)
}
defer conn.Close()
defer conn.Purge()
request := CustomHelo{
MessageType: "hello",
DeviceId: deviceId,
ChannelIds: []interface{}{t.channelId},
replies: make(chan client.Reply),
errors: make(chan error),
}
reply, err := conn.WriteRequest(request)
if t.statusCode >= 200 && t.statusCode < 300 {
if err != nil {
return fmt.Errorf("On handshake test %v, error writing request: %#v", t.name, err)
}
helo, ok := reply.(client.ServerHelo)
if !ok {
return fmt.Errorf("On handshake test %v, type assertion failed for reply: %#v", t.name, reply)
}
if helo.StatusCode != 200 {
return fmt.Errorf("On handshake test %v, unexpected status code: got %#v; want 200", t.name, helo.StatusCode)
}
// The Simple Push server requires the channelIDs field to be present in
// the handshake, but does not validate its contents, since any queued
// messages will be immediately flushed to the client.
if helo.DeviceId != deviceId {
return fmt.Errorf("On handshake test %v, mismatched device ID: got %#v; want %#v", t.name, helo.DeviceId, deviceId)
}
return nil
}
if err != io.EOF {
return fmt.Errorf("On handshake test %v, error writing request: got %#v; want io.EOF", t.name, err)
}
err = conn.Close()
clientErr, ok := err.(client.Error)
if !ok {
return fmt.Errorf("On handshake test %v, type assertion failed for close error: %#v", t.name, err)
}
if clientErr.Status() != t.statusCode {
return fmt.Errorf("On handshake test %v, unexpected close error status: got %#v; want %#v", t.name, clientErr.Status(), t.statusCode)
}
return nil
}
示例9: Subscribe
// Subscribe subscribes a client to a new channel.
func (c *Conn) Subscribe() (channelId, endpoint string, err error) {
if channelId, err = id.Generate(); err != nil {
return "", "", err
}
if endpoint, err = c.Register(channelId); err != nil {
return "", "", err
}
return
}
示例10: DialOrigin
func DialOrigin(origin string) (*Conn, error) {
socket, err := ws.Dial(origin, "", origin)
if err != nil {
return nil, err
}
id, err := id.Generate()
if err != nil {
return nil, err
}
return NewConn(socket, id, false), nil
}
示例11: TestMessageTypes
func TestMessageTypes(t *testing.T) {
longId, err := generateIdSize(64000)
if err != nil {
t.Fatalf("Error generating longId: %#v", err)
}
existingId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating existingId: %#v", err)
}
addExistsHook(existingId, true)
defer removeExistsHook(existingId)
missingId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating missingId: %#v", err)
}
addExistsHook(missingId, false)
defer removeExistsHook(missingId)
specialTypes := []typeTest{
{name: "long device ID", messageType: "hello", deviceId: longId, statusCode: 200, shouldReset: true},
{name: "long message type", messageType: longId, deviceId: validId, statusCode: 401},
{name: "existing device ID with channels", messageType: "hello", deviceId: existingId, statusCode: 200, shouldReset: false},
// Sending channel IDs with an unknown device ID should return a new device ID.
{name: "unknown device ID with channels", messageType: "hello", deviceId: missingId, statusCode: 200, shouldReset: true},
}
for _, test := range specialTypes {
if err := test.Run(); err != nil {
t.Error(err)
}
}
for _, test := range typeTests {
if err := test.Run(); err != nil {
t.Error(err)
}
}
}
示例12: TestMessageTypes
func TestMessageTypes(t *testing.T) {
longId, err := generateIdSize(64000)
if err != nil {
t.Fatalf("Error generating longId: %#v", err)
}
existingId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating existingId: %#v", err)
}
addExistsHook(existingId, true)
defer removeExistsHook(existingId)
missingId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating missingId: %#v", err)
}
addExistsHook(missingId, false)
defer removeExistsHook(missingId)
specialTypes := []typeTest{
{"long device ID", "hello", longId, 503, true},
{"long message type", longId, validId, 401, true},
{"existing device ID with channels", "hello", existingId, 200, false},
// Sending channel IDs with an unknown device ID should return a new device ID.
{"unknown device ID with channels", "hello", missingId, 200, true},
}
for _, test := range specialTypes {
if err := test.Run(); err != nil {
t.Error(err)
}
}
for _, test := range typeTests {
if err := test.Run(); err != nil {
t.Error(err)
}
}
}
示例13: TestDuplicateRegisterHandshake
func TestDuplicateRegisterHandshake(t *testing.T) {
deviceId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating device ID: %#v", err)
}
addExistsHook(deviceId, true)
defer removeExistsHook(deviceId)
channelId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating channel ID: %#v", err)
}
origin, err := testServer.Origin()
if err != nil {
t.Fatalf("Error initializing test server: %#v", err)
}
conn, err := client.DialOrigin(origin)
if err != nil {
t.Fatalf("Error dialing origin: %#v", err)
}
defer conn.Close()
defer conn.Purge()
actualId, err := conn.WriteHelo(deviceId, channelId)
if err != nil {
t.Fatalf("Error writing handshake request: %#v", err)
}
if actualId != deviceId {
t.Errorf("Mismatched device ID: got %#v; want %#v", actualId, deviceId)
}
if !AllowDupes {
return
}
endpoint, err := conn.Register(channelId)
if err != nil {
t.Fatalf("Error writing duplicate registration request: %#v", err)
}
if !isValidEndpoint(endpoint) {
t.Errorf("Invalid push endpoint for channel %#v: %#v", channelId, endpoint)
}
}
示例14: TestPrematureACK
func TestPrematureACK(t *testing.T) {
channelId, err := id.Generate()
if err != nil {
t.Fatalf("Error generating channel ID: %#v", err)
}
origin, err := testServer.Origin()
if err != nil {
t.Fatalf("Error initializing test server: %#v", err)
}
conn, err := client.DialOrigin(origin)
if err != nil {
t.Fatalf("Error dialing origin: %#v", err)
}
defer conn.Close()
defer conn.Purge()
conn.RegisterDecoder("ack", client.DecoderFunc(decodeServerInvalidACK))
updates := []client.Update{
client.Update{
ChannelId: channelId,
Version: time.Now().UTC().Unix(),
},
}
request := ClientInvalidACK{client.NewACK(updates, true)}
reply, err := conn.WriteRequest(request)
if err != nil {
t.Fatalf("Error writing acknowledgement: %#v", err)
}
if reply.Status() != 401 {
t.Errorf("Incorrect status code: got %#v, wanted 401", reply.Status())
}
if r, ok := reply.(ServerInvalidACK); ok {
if len(r.Updates) != len(updates) {
t.Errorf("Incorrect update count: got %#v; want %#v", len(r.Updates), len(updates))
} else {
for index, update := range r.Updates {
if update != updates[index] {
t.Errorf("On update %#v, got %#v; want %#v", index, update, updates[index])
}
}
}
} else {
t.Errorf("Type assertion failed for reply: %#v", reply)
}
// The connection should be closed by the push server after sending
// the error response.
if err = conn.Close(); err != io.EOF {
t.Fatalf("Unexpected close error: got %#v; want io.EOF", err)
}
}
示例15: ServeHTTP
// ServeHTTP implements http.Handler.ServeHTTP.
func (h *LogHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
receivedAt := time.Now()
// The `X-Request-Id` header is used by Heroku, restify, etc. to correlate
// logs for the same request.
requestID := req.Header.Get(HeaderID)
if !id.Valid(requestID) {
requestID, _ = id.Generate()
req.Header.Set(HeaderID, requestID)
}
writer := &logResponseWriter{ResponseWriter: res, StatusCode: http.StatusOK}
defer h.logResponse(writer, req, requestID, receivedAt)
h.Handler.ServeHTTP(writer, req)
}