Fail execution on duplicate item ids

This commit is contained in:
Tim Van Baak 2025-02-20 21:03:39 -08:00
parent e0c5bdca38
commit eb7191631f
2 changed files with 20 additions and 4 deletions

View File

@ -154,6 +154,8 @@ func Execute(
parseError := false parseError := false
stdoutDone := false stdoutDone := false
stderrDone := false stderrDone := false
duplicateItem := ""
itemIds := make(map[string]bool)
monitor: monitor:
for { for {
select { select {
@ -166,6 +168,16 @@ monitor:
logs = append(logs, msg) logs = append(logs, msg)
parseError = true parseError = true
} else { } else {
if itemIds[item.Id] {
msg := fmt.Sprintf("[%s: item] %s (duplicate)", source, item.Id)
log.Print(msg)
logs = append(logs, msg)
duplicateItem = item.Id
cmd.Cancel()
break monitor
}
itemIds[item.Id] = true
if postProcess != nil { if postProcess != nil {
item = postProcess(item) item = postProcess(item)
} }
@ -198,7 +210,12 @@ monitor:
} }
err = cmd.Wait() err = cmd.Wait()
if ctx.Err() == context.DeadlineExceeded { if duplicateItem != "" {
err = fmt.Errorf("returned item %s twice: %v", duplicateItem, err)
errItem = makeErrorItem(err, logs)
log.Printf("error: %v", err)
return nil, nil, errItem, err
} else if ctx.Err() == context.DeadlineExceeded {
err = fmt.Errorf("timed out after %v", timeout) err = fmt.Errorf("timed out after %v", timeout)
errItem = makeErrorItem(err, logs) errItem = makeErrorItem(err, logs)
log.Printf("error: %v", err) log.Printf("error: %v", err)

View File

@ -113,11 +113,10 @@ func TestExecute(t *testing.T) {
assertLen(t, res, 0) assertLen(t, res, 0)
}) })
// TODO maybe this *should* be an Execute error, via a map[string]bool
t.Run("DuplicateItemIds", func(t *testing.T) { t.Run("DuplicateItemIds", func(t *testing.T) {
res, err := execute([]string{"jq", "-cn", `["a", "a"] | .[] | {id: .}`}) res, err := execute([]string{"jq", "-cn", `["a", "a"] | .[] | {id: .}`})
assertNil(t, err) assertNotNil(t, err)
assertLen(t, res, 2) assertLen(t, res, 0)
}) })
t.Run("ActionNullValueOk", func(t *testing.T) { t.Run("ActionNullValueOk", func(t *testing.T) {