Skip to content

Commit cd2a071

Browse files
authored
feat: add GetOrInjectBaggage (#217)
* feat: add GetOrInjectBaggage * fix: improve test codecov * fix: TestServeIn_signalWatch is blocked on windows
1 parent 6c60918 commit cd2a071

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

ctxmeta/ctxmeta.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ func (m *MetadataSet) GetBaggage(ctx context.Context) *Baggage {
221221
return nil
222222
}
223223

224+
// GetOrInjectBaggage creates and returns Baggage. using Baggage found within the context.
225+
// If that doesn't exist it creates new Baggage. It also returns a context.Context
226+
// object built around the returned Baggage.
227+
func (m *MetadataSet) GetOrInjectBaggage(ctx context.Context) (*Baggage, context.Context) {
228+
if baggage := m.GetBaggage(ctx); baggage != nil {
229+
return baggage, ctx
230+
}
231+
return m.Inject(ctx)
232+
}
233+
224234
// Inject constructs a Baggage object and injects it into the provided context
225235
// under the default context key. Use the returned context for all further
226236
// operations. The returned Data can be queried at any point for metadata
@@ -233,3 +243,10 @@ func Inject(ctx context.Context) (*Baggage, context.Context) {
233243
func GetBaggage(ctx context.Context) *Baggage {
234244
return DefaultMetadata.GetBaggage(ctx)
235245
}
246+
247+
// GetOrInjectBaggage creates and returns Baggage. using Baggage found within the context.
248+
// If that doesn't exist it creates new Baggage. It also returns a context.Context
249+
// object built around the returned Baggage.
250+
func GetOrInjectBaggage(ctx context.Context) (*Baggage, context.Context) {
251+
return DefaultMetadata.GetOrInjectBaggage(ctx)
252+
}

ctxmeta/ctxmeta_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestContextMeta_crud(t *testing.T) {
3939
assert.ErrorIs(t, err, ErrNotFound)
4040
}
4141

42-
func TestContextMeta_ErrNoBaggae(t *testing.T) {
42+
func TestContextMeta_ErrNoBaggage(t *testing.T) {
4343
t.Parallel()
4444

4545
ctx := context.Background()
@@ -157,3 +157,23 @@ func TestMetadata_global(t *testing.T) {
157157
world, _ = baggage3.Get("hello")
158158
assert.Equal(t, "world", world)
159159
}
160+
161+
func TestMetadata_GetOrInjectBaggage(t *testing.T) {
162+
t.Parallel()
163+
164+
ctx := context.Background()
165+
baggage1, ctx := GetOrInjectBaggage(ctx)
166+
baggage1.Set("hello", "world")
167+
168+
baggage2 := GetBaggage(ctx)
169+
world, _ := baggage2.Get("hello")
170+
assert.Equal(t, "world", world)
171+
172+
baggage3 := DefaultMetadata.GetBaggage(ctx)
173+
world, _ = baggage3.Get("hello")
174+
assert.Equal(t, "world", world)
175+
176+
baggage4, _ := GetOrInjectBaggage(ctx)
177+
world, _ = baggage4.Get("hello")
178+
assert.Equal(t, "world", world)
179+
}

serve_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"os"
8+
"runtime"
89
"testing"
910
"time"
1011

@@ -21,16 +22,19 @@ func TestServeIn_signalWatch(t *testing.T) {
2122
assert.NoError(t, err)
2223

2324
t.Run("stop when signal received", func(t *testing.T) {
25+
if runtime.GOOS == "windows" {
26+
t.Skip("TestServeIn_signalWatch/stop_when_signal_received only works on unix")
27+
}
2428
var group run.Group
2529
group.Add(do, cancel)
2630
group.Add(func() error {
2731
time.Sleep(time.Second)
2832
p, err := os.FindProcess(os.Getpid())
2933
if err != nil {
30-
t.Skip("TestServeIn_signalWatch only works on unix")
34+
return err
3135
}
3236
if err := p.Signal(os.Interrupt); err != nil {
33-
t.Skip("TestServeIn_signalWatch only works on unix")
37+
return err
3438
}
3539
// trigger the signal twice should be ok.
3640
p.Signal(os.Interrupt)

0 commit comments

Comments
 (0)