github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/logs/noaa_message_queue_test.go (about)

     1  package logs_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/cf/api/logs"
     5  	"github.com/cloudfoundry/sonde-go/events"
     6  	"github.com/gogo/protobuf/proto"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("NoaaMessageQueue", func() {
    12  	It("sorts messages based on their timestamp, clearing after it's enumerated over", func() {
    13  		pq := NewNoaaMessageQueue()
    14  
    15  		msg3 := noaaMessageWithTime("message 3", 130)
    16  		msg2 := noaaMessageWithTime("message 2", 120)
    17  		msg4 := noaaMessageWithTime("message 4", 140)
    18  		msg1 := noaaMessageWithTime("message 1", 110)
    19  
    20  		pq.PushMessage(msg3)
    21  		pq.PushMessage(msg2)
    22  		pq.PushMessage(msg4)
    23  		pq.PushMessage(msg1)
    24  
    25  		var messages []*events.LogMessage
    26  
    27  		pq.EnumerateAndClear(func(m *events.LogMessage) {
    28  			messages = append(messages, m)
    29  		})
    30  
    31  		Expect(messages).To(Equal([]*events.LogMessage{
    32  			msg1,
    33  			msg2,
    34  			msg3,
    35  			msg4,
    36  		}))
    37  
    38  		var messagesAfter []*events.LogMessage
    39  
    40  		pq.EnumerateAndClear(func(m *events.LogMessage) {
    41  			messagesAfter = append(messagesAfter, m)
    42  		})
    43  
    44  		Expect(messagesAfter).To(BeEmpty())
    45  	})
    46  })
    47  
    48  func noaaMessageWithTime(messageString string, timestamp int) *events.LogMessage {
    49  	return generateNoaaMessage(messageString, int64(timestamp))
    50  }
    51  
    52  func generateNoaaMessage(messageString string, timestamp int64) *events.LogMessage {
    53  	messageType := events.LogMessage_OUT
    54  	sourceType := "DEA"
    55  	return &events.LogMessage{
    56  		Message:     []byte(messageString),
    57  		AppId:       proto.String("my-app-guid"),
    58  		MessageType: &messageType,
    59  		SourceType:  &sourceType,
    60  		Timestamp:   proto.Int64(timestamp),
    61  	}
    62  }