Skip to content

Commit 306c6d2

Browse files
committed
SCFF-6 added the modifications regarding the test and Event handling
1 parent c01f855 commit 306c6d2

9 files changed

Lines changed: 150 additions & 197 deletions

File tree

eventQueue/eventQueue.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package eventQueue
2+
3+
import (
4+
. "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
5+
)
6+
7+
//Node to put in queue
8+
type Node struct {
9+
event Event
10+
}
11+
12+
// Queue is a basic FIFO queue based on a circular list that resizes as needed.
13+
type Queue struct {
14+
nodes []*Node
15+
head int
16+
tail int
17+
count int
18+
}
19+
20+
func NewNode(event Event) *Node {
21+
return &Node{
22+
event: event,
23+
}
24+
}
25+
26+
func NewQueue(n []*Node) *Queue {
27+
return &Queue{
28+
nodes: n,
29+
}
30+
}
31+
32+
func (q *Queue) GetNode() []*Node {
33+
return q.nodes
34+
}
35+
36+
func (n *Node) GetNodeEvent() Event {
37+
return n.event
38+
}
39+
40+
// Push adds a node to the queue.
41+
func (q *Queue) Push(n *Node) {
42+
if q.head == q.tail && q.count > 0 {
43+
nodes := make([]*Node, len(q.nodes)*2)
44+
copy(nodes, q.nodes[q.head:])
45+
copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head])
46+
q.head = 0
47+
q.tail = len(q.nodes)
48+
q.nodes = nodes
49+
}
50+
q.nodes[q.tail] = n
51+
q.tail = (q.tail + 1) % len(q.nodes)
52+
q.count++
53+
}
54+
55+
// Pop removes and returns a node from the queue in first to last order.
56+
func (q *Queue) Pop() *Node {
57+
if q.count == 0 {
58+
return nil
59+
}
60+
node := q.nodes[q.head]
61+
q.head = (q.head + 1) % len(q.nodes)
62+
q.count--
63+
return node
64+
}

eventQueue/eventQueue_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package eventQueue
2+
3+
import (
4+
"testing"
5+
6+
. "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestQueueFIFO(t *testing.T) {
11+
12+
node1 := Node{
13+
event: Event{
14+
Fields: map[string]interface{}{
15+
"message_type": "OUT",
16+
"cf_app_id": "011",
17+
},
18+
Msg: "index [01]",
19+
},
20+
}
21+
node2 := Node{
22+
event: Event{
23+
Fields: map[string]interface{}{
24+
"message_type": "OUT",
25+
"cf_app_id": "022",
26+
},
27+
Msg: "index [02]",
28+
},
29+
}
30+
node3 := Node{
31+
event: Event{
32+
Fields: map[string]interface{}{
33+
"message_type": "OUT",
34+
"cf_app_id": "033",
35+
},
36+
Msg: "index [03]",
37+
},
38+
}
39+
40+
queue := Queue{
41+
nodes: make([]*Node, 3),
42+
}
43+
44+
queue.Push(&node1)
45+
queue.Push(&node2)
46+
queue.Push(&node3)
47+
48+
assert.Equal(t, queue.Pop().event.Msg, "index [01]", "")
49+
assert.Equal(t, queue.Pop().event.Msg, "index [02]", "")
50+
assert.Equal(t, queue.Pop().event.Msg, "index [03]", "")
51+
}

eventRouting/eventRouting_test.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

eventRouting/eventrouting.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/caching"
11+
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
1112
fevents "bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/events"
1213
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/sumoCFFirehose"
1314
"github.com/Sirupsen/logrus"
@@ -19,18 +20,18 @@ type EventRouting struct {
1920
selectedEvents map[string]bool
2021
selectedEventsCount map[string]uint64
2122
mutex *sync.Mutex
22-
sLAppender sumoCFFirehose.SumoCFFirehose //**
23-
//*log logging.Logging
23+
sLAppender sumoCFFirehose.SumoLogicAppender //**
24+
queue eventQueue.Queue
2425
}
2526

26-
func NewEventRouting(caching caching.Caching, sLAppender sumoCFFirehose.SumoCFFirehose) *EventRouting {
27+
func NewEventRouting(caching caching.Caching, sLAppender sumoCFFirehose.SumoLogicAppender, queue eventQueue.Queue) *EventRouting {
2728
return &EventRouting{
2829
CachingClient: caching,
2930
selectedEvents: make(map[string]bool),
3031
selectedEventsCount: make(map[string]uint64),
3132
sLAppender: sLAppender, //**
32-
//* log: logging,
33-
mutex: &sync.Mutex{},
33+
queue: queue,
34+
mutex: &sync.Mutex{},
3435
}
3536
}
3637

@@ -74,9 +75,14 @@ func (e *EventRouting) RouteEvent(msg *events.Envelope) {
7475
if ignored, hasIgnoredField := event.Fields["cf_ignored_app"]; ignored == true && hasIgnoredField {
7576
e.selectedEventsCount["ignored_app_message"]++
7677
} else {
77-
/*fmt.Println("This is the message field")
78-
fmt.Println(event.Msg)*/
79-
e.sLAppender.AppendLogs(event.Fields, event.Msg) //**/here we have to change the method for the one on sumoLogicAppender
78+
if e.queue.Pop() != nil { //if the queue is not empty
79+
fmt.Println("sendig event from queue to appender")
80+
e.sLAppender.AppendLogs(e.queue.Pop().GetNodeEvent()) // send to appender event from queue
81+
} else { //if the queue is empty, send the event to queue
82+
fmt.Println(event.Msg)
83+
fmt.Println("sendig event TO queue")
84+
e.queue.Push(eventQueue.NewNode(*event))
85+
}
8086
e.selectedEventsCount[eventType.String()]++
8187

8288
}
@@ -145,7 +151,7 @@ func (e *EventRouting) LogEventTotals(logTotalsTime time.Duration) {
145151
event, lastCount := e.getEventTotals(totalElapsedTime, elapsedTime, count)
146152
count = lastCount
147153
//*e.log.ShipEvents(event.Fields, event.Msg)
148-
e.sLAppender.AppendLogs(event.Fields, event.Msg)
154+
e.sLAppender.AppendLogs(*event)
149155
}
150156
}()
151157
}

events/events_suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package events_test
22

33
import (
4+
"testing"
5+
46
. "github.com/cloudfoundry/sonde-go/events"
57
. "github.com/onsi/ginkgo"
68
. "github.com/onsi/gomega"
7-
"testing"
89
)
910

1011
func TestEvents(t *testing.T) {

main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/caching"
10+
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventQueue"
1011
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/eventRouting"
1112
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/firehoseclient"
1213
"bitbucket.org/mcplusa-ondemand/firehose-to-sumologic/sumoCFFirehose"
@@ -62,8 +63,12 @@ func main() {
6263
} else {
6364
cachingClient = caching.NewCachingEmpty()
6465
}
66+
67+
//Creating queue
68+
queue := eventQueue.NewQueue(make([]*eventQueue.Node, 100))
69+
6570
//Creating Events
66-
events := eventRouting.NewEventRouting(cachingClient, loggingClientSumo)
71+
events := eventRouting.NewEventRouting(cachingClient, *loggingClientSumo, *queue)
6772
err := events.SetupEventRouting(*wantedEvents)
6873
if err != nil {
6974
log.Fatal("Error setting up event routing: ", err)
@@ -91,6 +96,7 @@ func main() {
9196
if /*loggingClientSumo.Connect() ||*/ debug {
9297

9398
fmt.Printf("Connected to Server! Connecting to Firehose... \n")
99+
94100
firehoseClient := firehoseclient.NewFirehoseNozzle(cfClient, events, firehoseConfig)
95101
err = firehoseClient.Start()
96102
fmt.Printf("I created the Firehose... \n")

sumoCFFirehose/sumoCFFirehose.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

sumoCFFirehose/sumoLog4gofakes/fake_SumoLog4go.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)