Skip to content

Commit a8e3f06

Browse files
committed
test: keep the example client releases reachable
gocritic's exitAfterDefer failed the lint job on the two examples this branch gave a `defer func() { _ = client.Close() }()`. log.Fatalf calls os.Exit, which does not unwind the stack, so every Fatalf placed after the defer would have skipped the close the branch added -- the leak the branch set out to fix, still leaking on the error path. Switch those calls to log.Panicf, which runs deferred functions on its way out. Examples have no *testing.T, so require.NoError and t.Cleanup are not available here; panicking is what the neighbouring osprom and osotel examples already do. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 0cf031b commit a8e3f06

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

opensearch_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func ExampleNewDefaultClient() {
5656

5757
_, err = client.Info(ctx, nil)
5858
if err != nil {
59-
log.Fatalf("Error getting the response: %s\n", err)
59+
log.Panicf("Error getting the response: %s\n", err)
6060
}
6161

6262
log.Print(client.Client.Transport.(*opensearchtransport.Transport).URLs())

opensearchutil/bulk_indexer_example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ExampleNewBulkIndexer() {
7575
FlushBytes: 5e+6, // The flush threshold in bytes (default: 5M)
7676
})
7777
if err != nil {
78-
log.Fatalf("Error creating the indexer: %s", err)
78+
log.Panicf("Error creating the indexer: %s", err)
7979
}
8080

8181
// Add an item to the indexer
@@ -116,20 +116,20 @@ func ExampleNewBulkIndexer() {
116116
},
117117
)
118118
if err != nil {
119-
log.Fatalf("Unexpected error: %s", err)
119+
log.Panicf("Unexpected error: %s", err)
120120
}
121121

122122
// Close the indexer channel and flush remaining items
123123
//
124124
if err := indexer.Close(context.Background()); err != nil {
125-
log.Fatalf("Unexpected error: %s", err)
125+
log.Panicf("Unexpected error: %s", err)
126126
}
127127

128128
// Report the indexer statistics
129129
//
130130
stats := indexer.Stats()
131131
if stats.NumFailed > 0 {
132-
log.Fatalf("Indexed [%d] documents with [%d] errors", stats.NumFlushed, stats.NumFailed)
132+
log.Panicf("Indexed [%d] documents with [%d] errors", stats.NumFlushed, stats.NumFailed)
133133
}
134134
log.Printf("Successfully indexed [%d] documents", stats.NumFlushed)
135135

0 commit comments

Comments
 (0)