Esempio n. 1
0
func TestServerError(t *testing.T) {
	reply, err := (&ninchatapi.DescribeChannel{
		ChannelId: pointer.String("1"),
	}).Invoke(nil)
	if reply != nil {
		t.Error(reply)
	}

	if err == nil {
		t.Fatal("no err")
	}

	event, ok := err.(*ninchatapi.Error)
	if !ok {
		t.Fatal(err)
	}

	if event.String() != "error" {
		t.Fatal(event)
	}

	if event.ErrorType != "channel_not_found" {
		t.Error(event.ErrorType)
	}

	if event.ChannelId == nil {
		t.Error("no channel_id")
	} else if *event.ChannelId != "1" {
		t.Error(*event.ChannelId)
	}
}
Esempio n. 2
0
func findLogStreamsSince(logService *cloudwatchlogs.CloudWatchLogs, startTime time.Time, endTime time.Time, output chan<- stream) {
	var (
		startTimestamp = startTime.UnixNano() / 1000000
		endTimestamp   = endTime.UnixNano() / 1000000
	)

	go func() {
		defer close(output)

		var nextToken *string

		for {
			logStreams, err := logService.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
				Descending:   pointer.Bool(true),
				LogGroupName: pointer.String(logGroupName),
				NextToken:    nextToken,
				OrderBy:      pointer.String("LastEventTime"),
			})
			if err != nil {
				panic(err)
			}

			for _, logStream := range logStreams.LogStreams {
				fmt.Fprintf(os.Stderr, "%s...%s %s\n", time.Unix(0, *logStream.FirstEventTimestamp*1000000), time.Unix(0, *logStream.LastEventTimestamp*1000000), *logStream.LogStreamName)

				if endTimestamp > 0 && *logStream.FirstEventTimestamp > endTimestamp {
					continue
				}

				if *logStream.LastEventTimestamp < startTimestamp {
					return
				}

				output <- stream{
					name: *logStream.LogStreamName,
				}
			}

			nextToken = logStreams.NextToken
		}
	}()

	return
}
Esempio n. 3
0
func TestCall(t *testing.T) {
	action := ninchatapi.DescribeChannel{
		ChannelId: pointer.String("1p255nth008"),
	}

	reply, err := action.Invoke(nil)
	if err != nil {
		t.Fatal(err)
	}

	if reply.RealmId != nil {
		t.Log(reply, reply.ChannelId, "realm", *reply.RealmId)
	} else {
		t.Log(reply, reply.ChannelId)
	}
}
Esempio n. 4
0
func testSession(t *testing.T, transport string) {
	params := map[string]interface{}{
		"message_types": []string{
			"*",
		},
	}

	session, events := openSession(t, transport, params)
	defer session.Close()

	sessionEvent := ninchatapi.NewSessionCreated(<-events)
	if sessionEvent.Id() <= 0 {
		t.Error(sessionEvent.Id())
	}

	messageEvent, err := (&ninchatapi.SendMessage{
		UserId:      &sessionEvent.UserId,
		MessageType: pointer.String("ninchat.com/text"),
		Payload: []ninchat.Frame{
			messageData,
		},
	}).Invoke(session)
	if err != nil {
		t.Fatal(err)
	}

	if messageEvent.Id() <= sessionEvent.Id() {
		t.Error(messageEvent.Id())
	}

	if len(messageEvent.Payload()) != 1 {
		t.Fatal("payload length")
	}

	if bytes.Compare(messageEvent.Payload()[0], messageData) != 0 {
		t.Error("payload content")
	}

	userEvent, err := (&ninchatapi.UpdateUser{
		PayloadAttrs: []string{"icon"},
		Payload: []ninchat.Frame{
			imageData,
		},
	}).Invoke(session)
	if err != nil {
		t.Fatal(err)
	}

	if userEvent.Id() <= messageEvent.Id() {
		t.Error(userEvent.Id())
	}

	if userEvent.UserAttrs == nil {
		t.Error("UserAttrs")
	} else if url := userEvent.UserAttrs.Iconurl; url == nil || *url == "" {
		t.Error("Iconurl")
	}

	deleteEvent, err := (&ninchatapi.DeleteUser{
		UserAuth: sessionEvent.UserAuth,
	}).Invoke(session)
	if err != nil {
		t.Fatal(err)
	}

	if deleteEvent.Id() <= userEvent.Id() {
		t.Error(deleteEvent.Id())
	}
}
Esempio n. 5
0
func load(logService *cloudwatchlogs.CloudWatchLogs, initial chan<- string, follow chan<- string, limit int, startTime time.Time, endTime time.Time, instanceId string, terminated bool) {
	initialParams := &cloudwatchlogs.GetLogEventsInput{
		LogGroupName:  pointer.String(logGroupName),
		LogStreamName: &instanceId,
	}

	if endTime.IsZero() {
		if !startTime.IsZero() {
			initialParams.StartFromHead = pointer.Bool(true)
			initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		} else {
			initialParams.EndTime = pointer.Int64(time.Now().UnixNano() / int64(time.Millisecond))
		}

		initialParams.Limit = pointer.Int64(int64(limit))
	} else {
		initialParams.StartFromHead = pointer.Bool(true)
		initialParams.StartTime = pointer.Int64(startTime.UnixNano() / int64(time.Millisecond))
		initialParams.EndTime = pointer.Int64(endTime.UnixNano() / int64(time.Millisecond))
	}

	logEvents, err := logService.GetLogEvents(initialParams)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
		initial <- ""
		return
	}

	for _, e := range logEvents.Events {
		if *e.Message != "" {
			initial <- formatMessage(e)
		}
	}

	initial <- ""

	if follow == nil || terminated {
		return
	}

	token := logEvents.NextForwardToken

	for {
		logEvents, err := logService.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
			LogGroupName:  pointer.String(logGroupName),
			LogStreamName: &instanceId,
			NextToken:     token,
			StartFromHead: pointer.Bool(true),
		})
		if err != nil {
			fmt.Fprintf(os.Stderr, "%s: %v\n", instanceId, err)
			continue
		}

		for _, e := range logEvents.Events {
			follow <- formatMessage(e)
		}

		token = logEvents.NextForwardToken

		time.Sleep(pollInterval)
	}
}