package riemann import ( "bytes" "encoding/json" "net" "net/http" "sync" "sync/atomic" "testing" "time" pb "go.iscode.ca/riemann/proto" ) func TestCorePipeline(t *testing.T) { core := NewCore() var matchedCount atomic.Int64 var totalCount atomic.Int64 // Build a pipeline: // Route events with service = "cpu" to the matched counter, // and count all incoming events in totalCount. core.Streams( CustomStream(func(e *Event) { totalCount.Add(1) }), Where(func(e *Event) bool { return e.Service == "cpu" }, CustomStream(func(e *Event) { matchedCount.Add(1) })), ) core.Send(&Event{Service: "cpu", Host: "web1"}) core.Send(&Event{Service: "cpu", Host: "web2"}) core.Send(&Event{Service: "mem", Host: "web1"}) if totalCount.Load() != 3 { t.Errorf("expected total count 3, got %d", totalCount.Load()) } if matchedCount.Load() != 2 { t.Errorf("expected matched count 2, got %d", matchedCount.Load()) } } func TestCoreRollup(t *testing.T) { var mu sync.Mutex var received []*Event rollupStream := Rollup(20*time.Millisecond, CustomStream(func(e *Event) { mu.Lock() received = append(received, e) mu.Unlock() })) rollupStream.Send(&Event{Service: "cpu", Metric: 1.0}) rollupStream.Send(&Event{Service: "cpu", Metric: 2.0}) rollupStream.Send(&Event{Service: "cpu", Metric: 3.0}) // Wait for rollup to flush time.Sleep(50 * time.Millisecond) mu.Lock() l := len(received) mu.Unlock() if l != 1 { t.Fatalf("expected exactly 1 rollup event, got %d", l) } mu.Lock() lastVal := received[0].Metric.(float64) mu.Unlock() if lastVal != 3.0 { t.Errorf("expected rolled up metric to be the latest value (3.0), got %v", lastVal) } } func TestCoreNetworkStreamIntegration(t *testing.T) { core := NewCore() core.AddTCPServer("127.0.0.1:0") core.AddWebServer("127.0.0.1:0") var tcpCount atomic.Int64 var webCount atomic.Int64 // Build pipeline routing events to index and specific metric counters core.Streams( IndexStream(core.Index), Where(func(e *Event) bool { return e.Service == "tcp-service" }, CustomStream(func(e *Event) { tcpCount.Add(1) })), Where(func(e *Event) bool { return e.Service == "web-service" }, CustomStream(func(e *Event) { webCount.Add(1) })), ) err := core.Start() if err != nil { t.Fatalf("failed to start Core: %v", err) } defer core.Stop() // 1. Send TCP event tcpAddr := core.TCPServer.Addr().String() conn, err := netDialTCP(tcpAddr) if err != nil { t.Fatalf("failed to connect to TCP server: %v", err) } defer func() { _ = conn.Close() }() tcpEvent := &Event{Host: "localhost", Service: "tcp-service", State: "ok"} err = writeMsg(conn, &pb.Msg{ Events: []*pb.Event{ EventToProto(tcpEvent), }, }) if err != nil { t.Fatalf("failed to send TCP event: %v", err) } _, _ = readMsg(conn) // read ack // 2. Send HTTP Web event webAddr := core.WebServer.Addr() webEvent := &Event{Host: "localhost", Service: "web-service", State: "ok"} payload, _ := json.Marshal(webEvent) _, err = http.Post("http://"+webAddr+"/events", "application/json", bytes.NewBuffer(payload)) if err != nil { t.Fatalf("failed to send HTTP event: %v", err) } // Verify events went through the stream processors time.Sleep(50 * time.Millisecond) if tcpCount.Load() != 1 { t.Errorf("expected 1 TCP event through pipeline, got %d", tcpCount.Load()) } if webCount.Load() != 1 { t.Errorf("expected 1 HTTP event through pipeline, got %d", webCount.Load()) } // Verify both exist in index (since IndexStream was registered) _, foundTCP := core.Index.Lookup("localhost", "tcp-service") _, foundWeb := core.Index.Lookup("localhost", "web-service") if !foundTCP || !foundWeb { t.Errorf("expected both events indexed, tcp=%t web=%t", foundTCP, foundWeb) } } // Helpers for tests func netDialTCP(addr string) (net.Conn, error) { return net.Dial("tcp", addr) }