Skip to content

Commit

Permalink
(misc) Use ginkgo provided contexts
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Feb 12, 2025
1 parent bd08272 commit cea5d10
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 93 deletions.
8 changes: 3 additions & 5 deletions aagent/machine/machine_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2019-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -162,15 +162,13 @@ var _ = Describe("Aagent/Machine", func() {
})

Describe("Start", func() {
It("Should start the machine using the manager", func() {
It("Should start the machine using the manager", func(ctx context.Context) {
wg := &sync.WaitGroup{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

manager.EXPECT().SetMachine(gomock.AssignableToTypeOf(&Machine{}))
machine, err = FromYAML("testdata/machine.yaml", manager)
Expect(err).ToNot(HaveOccurred())
manager.EXPECT().Run(gomock.AssignableToTypeOf(ctx), wg)
manager.EXPECT().Run(gomock.Any(), wg)

machine.SplayStart = 0

Expand Down
12 changes: 3 additions & 9 deletions aagent/watchers/metricwatcher/metric_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2024, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -68,7 +68,7 @@ var _ = Describe("MetricWatcher", func() {
})

Describe("performWatch", func() {
It("Should run the script and correctly parse nagios style metrics", func() {
It("Should run the script and correctly parse nagios style metrics", func(ctx context.Context) {
if runtime.GOOS == "windows" {
Skip("not tested on windows yet")
}
Expand All @@ -94,14 +94,11 @@ var _ = Describe("MetricWatcher", func() {
Expect(err).ToNot(HaveOccurred())
watch = wi.(*Watcher)

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

watch.performWatch(ctx)
Expect(handled).To(BeTrue())
})

It("Should run the script and correctly parse choria style metrics", func() {
It("Should run the script and correctly parse choria style metrics", func(ctx context.Context) {
if runtime.GOOS == "windows" {
Skip("not tested on windows yet")
}
Expand All @@ -126,9 +123,6 @@ var _ = Describe("MetricWatcher", func() {
Expect(err).ToNot(HaveOccurred())
watch = wi.(*Watcher)

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

watch.performWatch(ctx)
Expect(handled).To(BeTrue())
})
Expand Down
4 changes: 2 additions & 2 deletions broker/federation/choria_nats_egest_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2017-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -30,7 +30,6 @@ var _ = Describe("Choria NATS Egest", func() {
)

BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
var gblogger *log.Logger
logbuf, gblogger = testutil.GbytesLogger(log.DebugLevel)
logger = log.NewEntry(gblogger)
Expand Down Expand Up @@ -59,6 +58,7 @@ var _ = Describe("Choria NATS Egest", func() {
manager = &stubConnectionManager{}
connector.connection = manager

ctx, cancel = context.WithCancel(context.Background())
go connector.Run(ctx)
})

Expand Down
12 changes: 3 additions & 9 deletions client/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Client", func() {
})

Describe("Request", func() {
It("Should support fire and forget requests", func() {
It("Should support fire and forget requests", func(ctx context.Context) {
pubStarted := false
pubEnded := false

Expand All @@ -70,9 +70,6 @@ var _ = Describe("Client", func() {
Expect(err).ToNot(HaveOccurred())
msg.SetReplyTo("custom")

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

conn.EXPECT().Publish(gomock.Any()).AnyTimes()

err = client.Request(ctx, msg, nil)
Expand All @@ -82,7 +79,7 @@ var _ = Describe("Client", func() {
Expect(pubEnded).To(BeTrue())
})

It("Should perform the request and call the handler for each reply", func() {
It("Should perform the request and call the handler for each reply", func(ctx context.Context) {
seen := []string{}
pubStarted := false
pubEnded := false
Expand All @@ -109,9 +106,6 @@ var _ = Describe("Client", func() {

msg.SetReplyTo(fmt.Sprintf("%s.reply.%s.%s", msg.Collective(), fmt.Sprintf("%x", md5.Sum([]byte(msg.CallerID()))), msg.RequestID()))

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

conn.EXPECT().QueueSubscribe(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), client.replies).
AnyTimes().
Do(func(ctx context.Context, name string, subject string, group string, output chan inter.ConnectorMessage) {
Expand Down
22 changes: 9 additions & 13 deletions providers/agent/mcorpc/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -43,8 +43,6 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
rpc *RPC
mockctl *gomock.Controller
cl *MockChoriaClient
ctx context.Context
cancel func()
err error
)

Expand Down Expand Up @@ -72,11 +70,9 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(err).ToNot(HaveOccurred())

rpc.cl = cl
ctx, cancel = context.WithCancel(context.Background())
})

AfterEach(func() {
cancel()
mockctl.Finish()
})

Expand Down Expand Up @@ -144,7 +140,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
})

Describe("Do", func() {
It("Should only accept DDLs for the requested agent", func() {
It("Should only accept DDLs for the requested agent", func(ctx context.Context) {
ddl := &agent.DDL{
Metadata: &agents.Metadata{
Name: "backplane",
Expand All @@ -171,7 +167,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(err).To(MatchError("the DDL does not describe the package agent"))
})

It("Should perform the request", func() {
It("Should perform the request", func(ctx context.Context) {
reqid := ""
handled := 0

Expand Down Expand Up @@ -248,7 +244,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
req, err := fw.NewRequestFromSecureRequest(sreq)
Expect(err).ToNot(HaveOccurred())

rpchandler := rpc.handlerFactory(ctx, cancel, rpc.opts.totalStats)
rpchandler := rpc.handlerFactory(ctx, func() {}, rpc.opts.totalStats)

for i := 0; i < 2; i++ {
reply, err := v1.NewReply(req, fmt.Sprintf("test.sender.%d", i))
Expand Down Expand Up @@ -309,7 +305,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(stats.Agent()).To(Equal("package"))
})

It("Should support discovery callbacks and limits", func() {
It("Should support discovery callbacks and limits", func(ctx context.Context) {
cl.EXPECT().Request(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(func(ctx context.Context, msg inter.Message, handler client.Handler) {
Expect(msg.DiscoveredHosts()).To(Equal([]string{"host1"}))
})
Expand All @@ -333,7 +329,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(limitedCnt).To(Equal(1))
})

It("Should interruptable by the discovery callback", func() {
It("Should interruptable by the discovery callback", func(ctx context.Context) {
_, err := rpc.Do(ctx, "test_action", request{Testing: true},
Targets([]string{"host1", "host2", "host3", "host4"}),
LimitSize("1"),
Expand All @@ -346,7 +342,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(err).To(MatchError("simulated"))
})

It("Should support batched mode", func() {
It("Should support batched mode", func(ctx context.Context) {
batch1 := cl.EXPECT().Request(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(func(ctx context.Context, msg inter.Message, handler client.Handler) {
Expect(msg.DiscoveredHosts()).To(Equal([]string{"host1", "host2"}))
})
Expand All @@ -358,7 +354,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
rpc.Do(ctx, "test_action", request{Testing: true}, Targets([]string{"host1", "host2", "host3", "host4"}), InBatches(2, -1))
})

It("Should support making requests without processing replies unbatched", func() {
It("Should support making requests without processing replies unbatched", func(ctx context.Context) {
cl.EXPECT().Request(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(func(ctx context.Context, msg inter.Message, handler client.Handler) {
Expect(msg.DiscoveredHosts()).To(Equal([]string{"host1", "host2"}))
Expect(msg.ReplyTo()).To(Equal("custom.reply.to"))
Expand All @@ -369,7 +365,7 @@ var _ = Describe("Providers/Agent/McoRPC/Client", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Should support making requests without processing replies batched", func() {
It("Should support making requests without processing replies batched", func(ctx context.Context) {
batch1 := cl.EXPECT().Request(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Do(func(ctx context.Context, msg inter.Message, handler client.Handler) {
Expect(msg.DiscoveredHosts()).To(Equal([]string{"host1"}))
Expect(msg.ReplyTo()).To(Equal("custom.reply.to"))
Expand Down
22 changes: 5 additions & 17 deletions providers/agent/mcorpc/external/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -204,10 +204,7 @@ var _ = Describe("McoRPC/External", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Should handle a missing executable", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

It("Should handle a missing executable", func(ctx context.Context) {
prov.paths["ginkgo_missing"] = ddl.SourceLocation
ddl.Metadata.Name = "ginkgo_missing"
rep := &mcorpc.Reply{}
Expand All @@ -222,14 +219,11 @@ var _ = Describe("McoRPC/External", func() {
Expect(rep.Statuscode).To(Equal(mcorpc.Aborted))
})

It("Should handle execution failures", func() {
It("Should handle execution failures", func(ctx context.Context) {
if runtime.GOOS == "windows" {
Skip("Windows TODO")
}

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

prov.paths["ginkgo_abort"] = ddl.SourceLocation
ddl.Metadata.Name = "ginkgo_abort"
rep := &mcorpc.Reply{}
Expand All @@ -244,10 +238,7 @@ var _ = Describe("McoRPC/External", func() {
Expect(rep.Statuscode).To(Equal(mcorpc.Aborted))
})

It("Should validate the input before executing the agent", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

It("Should validate the input before executing the agent", func(ctx context.Context) {
prov.paths["ginkgo_abort"] = ddl.SourceLocation
ddl.Metadata.Name = "ginkgo_abort"
rep := &mcorpc.Reply{}
Expand All @@ -262,14 +253,11 @@ var _ = Describe("McoRPC/External", func() {
Expect(rep.Statuscode).To(Equal(mcorpc.Aborted))
})

It("Should execute the correct request binary with the correct input and set defaults on the reply", func() {
It("Should execute the correct request binary with the correct input and set defaults on the reply", func(ctx context.Context) {
if runtime.GOOS == "windows" {
Skip("Windows TODO")
}

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

rep := &mcorpc.Reply{}
req := &mcorpc.Request{
Agent: "ginkgo",
Expand Down
17 changes: 6 additions & 11 deletions providers/agent/mcorpc/external/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2020-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -33,8 +33,6 @@ var _ = Describe("McoRPC/External", func() {
conn *imock.MockConnector
cfg *config.Config
prov *Provider
ctx context.Context
cancel context.CancelFunc
)

BeforeEach(func() {
Expand Down Expand Up @@ -65,10 +63,7 @@ var _ = Describe("McoRPC/External", func() {
}
prov.log.Logger.SetLevel(logrus.DebugLevel)

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

DeferCleanup(func() {
cancel()
mockctl.Finish()
})
})
Expand Down Expand Up @@ -102,14 +97,14 @@ var _ = Describe("McoRPC/External", func() {
Expect(err).ToNot(HaveOccurred())
}

It("Should register new agents", func() {
It("Should register new agents", func(ctx context.Context) {
mgr.EXPECT().RegisterAgent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(3)
Expect(prov.agents).To(BeEmpty())
Expect(prov.reconcileAgents(ctx, mgr, conn)).To(Succeed())
Expect(prov.agents).To(HaveLen(3))
})

It("Should upgrade changed agents", func() {
It("Should upgrade changed agents", func(ctx context.Context) {
fileChangeGrace = 0

copyAgentFile("one.json")
Expand Down Expand Up @@ -139,7 +134,7 @@ var _ = Describe("McoRPC/External", func() {
Expect(prov.agents[0].Metadata.Version).To(Equal("6.0.0"))
})

It("Should remove orphaned agents", func() {
It("Should remove orphaned agents", func(ctx context.Context) {
copyAgentFile("one.json")
copyAgentFile("go_agent.json")
prov.cfg.Choria.RubyLibdir = []string{td}
Expand All @@ -156,7 +151,7 @@ var _ = Describe("McoRPC/External", func() {
Expect(prov.agents[0].Metadata.Name).To(Equal("echo"))
})

It("Should work in sequence", func() {
It("Should work in sequence", func(ctx context.Context) {
fileChangeGrace = 0
prov.cfg.Choria.RubyLibdir = []string{td}

Expand Down Expand Up @@ -206,7 +201,7 @@ var _ = Describe("McoRPC/External", func() {
})

Describe("Agents", func() {
It("Should return all the agent ddls", func() {
It("Should return all the agent ddls", func(ctx context.Context) {
mgr.EXPECT().RegisterAgent(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
Expect(prov.reconcileAgents(ctx, mgr, conn)).To(Succeed())

Expand Down
6 changes: 3 additions & 3 deletions providers/agent/mcorpc/golang/provision/shutdown_action.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2022-2025, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -7,7 +7,7 @@ package provision
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"time"

"github.com/choria-io/go-choria/build"
Expand Down Expand Up @@ -37,7 +37,7 @@ func shutdownAction(ctx context.Context, req *mcorpc.Request, reply *mcorpc.Repl
return
}

splay := time.Duration(rand.Intn(10)) * time.Second
splay := time.Duration(rand.IntN(10)) * time.Second
agent.Log.Warnf("Shutting server down via request %s from %s (%s) with splay %v", req.RequestID, req.CallerID, req.SenderID, splay)

go shutdownCb(splay, agent.ServerInfoSource, agent.Log)
Expand Down
Loading

0 comments on commit cea5d10

Please sign in to comment.