Skip to content

Commit

Permalink
Convert tests to go test
Browse files Browse the repository at this point in the history
  • Loading branch information
sjberman committed Jan 22, 2025
1 parent bbcf56f commit 40cc29b
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 303 deletions.
166 changes: 87 additions & 79 deletions internal/mode/static/nginx/agent/broadcast/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,105 @@ import (
"context"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/agent/broadcast"
)

func TestBroadcaster(t *testing.T) {
func TestSubscribe(t *testing.T) {
t.Parallel()
RegisterFailHandler(Fail)
RunSpecs(t, "Broadcaster Suite")
g := NewWithT(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

broadcaster := broadcast.NewDeploymentBroadcaster(ctx)

subscriber := broadcaster.Subscribe()
g.Expect(subscriber.ID).NotTo(BeEmpty())

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

go func() {
result := broadcaster.Send(message)
g.Expect(result).To(BeTrue())
}()

g.Eventually(subscriber.ListenCh).Should(Receive(Equal(message)))
}

var _ = Describe("Broadcaster", func() {
var (
ctx context.Context
cancel context.CancelFunc
broadcaster broadcast.Broadcaster
)

BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background()) //nolint:fatcontext // ignore for test
broadcaster = broadcast.NewDeploymentBroadcaster(ctx)
})

AfterEach(func() {
cancel()
})

It("should allow a listener to subscribe and receive messages", func() {
subscriber := broadcaster.Subscribe()
Expect(subscriber.ID).NotTo(BeEmpty())

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

go func() {
result := broadcaster.Send(message)
Expect(result).To(BeTrue())
}()

Eventually(subscriber.ListenCh).Should(Receive(Equal(message)))
})

It("should send messages to all subscribers and wait for responses", func() {
subscriber1 := broadcaster.Subscribe()
subscriber2 := broadcaster.Subscribe()

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

go func() {
result := broadcaster.Send(message)
Expect(result).To(BeTrue())
}()

Eventually(subscriber1.ListenCh).Should(Receive(Equal(message)))
Eventually(subscriber2.ListenCh).Should(Receive(Equal(message)))

subscriber1.ResponseCh <- struct{}{}
subscriber2.ResponseCh <- struct{}{}
})

It("should return false if there are no listeners", func() {
message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}
func TestSubscribe_MultipleListeners(t *testing.T) {
t.Parallel()
g := NewWithT(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

broadcaster := broadcast.NewDeploymentBroadcaster(ctx)

subscriber1 := broadcaster.Subscribe()
subscriber2 := broadcaster.Subscribe()

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

go func() {
result := broadcaster.Send(message)
Expect(result).To(BeFalse())
})
g.Expect(result).To(BeTrue())
}()

g.Eventually(subscriber1.ListenCh).Should(Receive(Equal(message)))
g.Eventually(subscriber2.ListenCh).Should(Receive(Equal(message)))

subscriber1.ResponseCh <- struct{}{}
subscriber2.ResponseCh <- struct{}{}
}

func TestSubscribe_NoListeners(t *testing.T) {
t.Parallel()
g := NewWithT(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

broadcaster := broadcast.NewDeploymentBroadcaster(ctx)

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

result := broadcaster.Send(message)
g.Expect(result).To(BeFalse())
}

func TestCancelSubscription(t *testing.T) {
t.Parallel()
g := NewWithT(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

It("should remove a subscriber from the list", func() {
subscriber := broadcaster.Subscribe()
broadcaster := broadcast.NewDeploymentBroadcaster(ctx)

broadcaster.CancelSubscription(subscriber.ID)
subscriber := broadcaster.Subscribe()

message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}
broadcaster.CancelSubscription(subscriber.ID)

go func() {
result := broadcaster.Send(message)
Expect(result).To(BeFalse())
}()
message := broadcast.NginxAgentMessage{
ConfigVersion: "v1",
Type: broadcast.ConfigApplyRequest,
}

Consistently(subscriber.ListenCh).ShouldNot(Receive())
})
})
go func() {
result := broadcaster.Send(message)
g.Expect(result).To(BeFalse())
}()

g.Consistently(subscriber.ListenCh).ShouldNot(Receive())
}
183 changes: 99 additions & 84 deletions internal/mode/static/nginx/agent/grpc/connections_test.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,108 @@
package grpc_test

import (
. "github.com/onsi/ginkgo/v2"
"testing"

. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"

agentgrpc "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/agent/grpc"
)

var _ = Describe("ConnectionsTracker", func() {
var tracker agentgrpc.ConnectionsTracker

BeforeEach(func() {
tracker = agentgrpc.NewConnectionsTracker()
})

It("should add a connection to the tracker", func() {
conn := agentgrpc.Connection{
PodName: "pod1",
InstanceID: "instance1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

trackedConn := tracker.GetConnection("key1")
Expect(trackedConn).To(Equal(conn))
})

It("should return an empty connection if the key does not exist", func() {
trackedConn := tracker.GetConnection("nonexistent")
Expect(trackedConn).To(Equal(agentgrpc.Connection{}))
})

It("should return true if the connection is ready", func() {
conn := agentgrpc.Connection{
PodName: "pod1",
InstanceID: "instance1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

trackedConn, ready := tracker.ConnectionIsReady("key1")
Expect(ready).To(BeTrue())
Expect(trackedConn).To(Equal(conn))
})

It("should return false if the connection is not ready when instanceID is not specified", func() {
conn := agentgrpc.Connection{
PodName: "pod1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

_, ready := tracker.ConnectionIsReady("key1")
Expect(ready).To(BeFalse())
})

It("should set the instance ID for a connection", func() {
conn := agentgrpc.Connection{
PodName: "pod1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

_, ready := tracker.ConnectionIsReady("key1")
Expect(ready).To(BeFalse())

tracker.SetInstanceID("key1", "instance1")

trackedConn, ready := tracker.ConnectionIsReady("key1")
Expect(ready).To(BeTrue())
Expect(trackedConn.InstanceID).To(Equal("instance1"))
})

It("should remove all connections for a given parent", func() {
parent := types.NamespacedName{Namespace: "default", Name: "parent1"}
conn1 := agentgrpc.Connection{PodName: "pod1", InstanceID: "instance1", Parent: parent}
conn2 := agentgrpc.Connection{PodName: "pod2", InstanceID: "instance2", Parent: parent}

parent2 := types.NamespacedName{Namespace: "default", Name: "parent2"}
conn3 := agentgrpc.Connection{PodName: "pod3", InstanceID: "instance3", Parent: parent2}

tracker.Track("key1", conn1)
tracker.Track("key2", conn2)
tracker.Track("key3", conn3)

tracker.UntrackConnectionsForParent(parent)
Expect(tracker.GetConnection("key1")).To(Equal(agentgrpc.Connection{}))
Expect(tracker.GetConnection("key2")).To(Equal(agentgrpc.Connection{}))
Expect(tracker.GetConnection("key3")).To(Equal(conn3))
})
})
func TestGetConnection(t *testing.T) {
t.Parallel()
g := NewWithT(t)

tracker := agentgrpc.NewConnectionsTracker()

conn := agentgrpc.Connection{
PodName: "pod1",
InstanceID: "instance1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

trackedConn := tracker.GetConnection("key1")
g.Expect(trackedConn).To(Equal(conn))

nonExistent := tracker.GetConnection("nonexistent")
g.Expect(nonExistent).To(Equal(agentgrpc.Connection{}))
}

func TestConnectionIsReady(t *testing.T) {
t.Parallel()
g := NewWithT(t)

tracker := agentgrpc.NewConnectionsTracker()

conn := agentgrpc.Connection{
PodName: "pod1",
InstanceID: "instance1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

trackedConn, ready := tracker.ConnectionIsReady("key1")
g.Expect(ready).To(BeTrue())
g.Expect(trackedConn).To(Equal(conn))
}

func TestConnectionIsNotReady(t *testing.T) {
t.Parallel()
g := NewWithT(t)

tracker := agentgrpc.NewConnectionsTracker()

conn := agentgrpc.Connection{
PodName: "pod1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

_, ready := tracker.ConnectionIsReady("key1")
g.Expect(ready).To(BeFalse())
}

func TestSetInstanceID(t *testing.T) {
t.Parallel()
g := NewWithT(t)

tracker := agentgrpc.NewConnectionsTracker()
conn := agentgrpc.Connection{
PodName: "pod1",
Parent: types.NamespacedName{Namespace: "default", Name: "parent1"},
}
tracker.Track("key1", conn)

_, ready := tracker.ConnectionIsReady("key1")
g.Expect(ready).To(BeFalse())

tracker.SetInstanceID("key1", "instance1")

trackedConn, ready := tracker.ConnectionIsReady("key1")
g.Expect(ready).To(BeTrue())
g.Expect(trackedConn.InstanceID).To(Equal("instance1"))
}

func TestUntrackConnectionsForParent(t *testing.T) {
t.Parallel()
g := NewWithT(t)

tracker := agentgrpc.NewConnectionsTracker()

parent := types.NamespacedName{Namespace: "default", Name: "parent1"}
conn1 := agentgrpc.Connection{PodName: "pod1", InstanceID: "instance1", Parent: parent}
conn2 := agentgrpc.Connection{PodName: "pod2", InstanceID: "instance2", Parent: parent}

parent2 := types.NamespacedName{Namespace: "default", Name: "parent2"}
conn3 := agentgrpc.Connection{PodName: "pod3", InstanceID: "instance3", Parent: parent2}

tracker.Track("key1", conn1)
tracker.Track("key2", conn2)
tracker.Track("key3", conn3)

tracker.UntrackConnectionsForParent(parent)
g.Expect(tracker.GetConnection("key1")).To(Equal(agentgrpc.Connection{}))
g.Expect(tracker.GetConnection("key2")).To(Equal(agentgrpc.Connection{}))
g.Expect(tracker.GetConnection("key3")).To(Equal(conn3))
}
Loading

0 comments on commit 40cc29b

Please sign in to comment.