Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/await all subscriptions before publishing #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var (
resultChan = make(chan Result)
ctlChan = make(chan string, *argNumClients)
stopWaitLoop = false
randomSource = rand.New(rand.NewSource(time.Now().UnixNano()))

Expand Down Expand Up @@ -51,7 +52,8 @@ var (
argKey = flag.String("key", "", "client private key for authentication, if required by server.")
argCert = flag.String("cert", "", "client certificate for authentication, if required by server.")
argPauseBetweenMessages = flag.String("pause-between-messages", "0s", "Adds a pause between sending messages to simulate sensors sending messages infrequently")
argTopicBasePath = flag.String("topic-base-path", "", "topic base path, if empty the default is internal/mqtt-stresser")
argTopicBasePath = flag.String("topic-base-path", "", "topic base path, if empty the default is internal/mqtt-stresser")
argAwaitClients = flag.Bool("await-clients", false, "wait for all clients to connect before publishing")
)

type Result struct {
Expand Down Expand Up @@ -185,7 +187,7 @@ func main() {
if strings.HasPrefix(*argConstantPayload, "@") {
verboseLogger.Printf("Set constant payload from file %s\n", *argConstantPayload)
payloadGenerator = filePayloadGenerator(*argConstantPayload)
}else {
} else {
verboseLogger.Printf("Set constant payload to %s\n", *argConstantPayload)
payloadGenerator = constantPayloadGenerator(*argConstantPayload)
}
Expand Down Expand Up @@ -268,6 +270,9 @@ func main() {
}
}

if !*argAwaitClients {
ctlChan <- StartPublishingEvent
}
go (&Worker{
WorkerId: cid,
BrokerUrl: *argBrokerUrl,
Expand All @@ -288,6 +293,7 @@ func main() {
}
fmt.Printf("%d worker started\n", *argNumClients)

subEvents := 0
finEvents := 0

results := make([]Result, *argNumClients)
Expand Down Expand Up @@ -316,6 +322,16 @@ func main() {
}
}

if msg.Event == SubscribedEvent {
subEvents++

if *argAwaitClients && subEvents == *argNumClients {
for i := 0; i < *argNumClients; i++ {
ctlChan <- StartPublishingEvent
}
}
}

case <-testCtx.Done():
switch testCtx.Err().(type) {
case TimeoutError:
Expand Down
2 changes: 2 additions & 0 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
AbortedEvent = "Aborted"
CompletedEvent = "Completed"
ProgressReportEvent = "ProgressReport"
SubscribedEvent = "Subscribed"
StartPublishingEvent = "StartPublishing"
)

type Summary struct {
Expand Down
7 changes: 7 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func (w *Worker) Run(ctx context.Context) {

return
}
resultChan <- Result{
WorkerId: w.WorkerId,
Event: SubscribedEvent,
}

for <-ctlChan != StartPublishingEvent {
}

publisher := mqtt.NewClient(publisherOptions)
verboseLogger.Printf("[%d] connecting publisher\n", w.WorkerId)
Expand Down