Atlas › Test

metadata_survives_context_cancellation

Exact test identity: go.temporal.io/server/common/contextutil/TestMetadataContextWithContextCancellation/metadata_survives_context_cancellation

Package
go.temporal.io/server/common/contextutil
Suite / test hierarchy
TestMetadataContextWithContextCancellation/metadata_survives_context_cancellation
Test
metadata_survives_context_cancellation
Introduced at
retrieves_existing_value, retrieves_nil_value_correctly, +5 Frontier kind: Test frontier
Covered ranges
8
Covered lines
29
Covered files
1

Co-introduced tests

6 other tests enter at the same concept.

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/contextutil/metadata.go 29 covered LOC · 8 ranges

Open complete file

119
120 // getMetadataContext extracts metadata context from golang context.
121 > func getMetadataContext(ctx context.Context) *metadataContext { metadata.go
122 > metadataCtx := ctx.Value(metadataCtxKey)
123 > if metadataCtx == nil {
124 return nil
125 }
126 > mc, ok := metadataCtx.(*metadataContext) metadata.go
127 > if !ok {
128 return nil
129 }
130 > return mc metadata.go
131 }
132
133 // WithMetadataContext adds a metadata context to the given context.
134 > func WithMetadataContext(ctx context.Context) context.Context { metadata.go
135 > metadataCtx := &metadataContext{
136 > Metadata: make(map[string]any),
137 > MarkedActivityIDs: make(map[string]struct{}),
138 > }
139 > return context.WithValue(ctx, metadataCtxKey, metadataCtx)
140 > }
141
142 // ContextHasMetadata returns true if the context has metadata support.
147
148 // ContextMetadataSet sets a metadata key-value pair in the context, overwriting any existing value.
149 > func ContextMetadataSet(ctx context.Context, key string, value any) bool { metadata.go
150 > metadataCtx := getMetadataContext(ctx)
151 > if metadataCtx == nil {
152 return false
153 }
154
155 > metadataCtx.Lock() metadata.go
156 > defer metadataCtx.Unlock()
157 >
158 > metadataCtx.Metadata[key] = value
159 > return true
160 }
161
162 // ContextMetadataGet retrieves a metadata value from the context.
163 > func ContextMetadataGet(ctx context.Context, key string) (any, bool) { metadata.go
164 > metadataCtx := getMetadataContext(ctx)
165 > if metadataCtx == nil {
166 return nil, false
167 }
168
169 > metadataCtx.Lock() metadata.go
170 > defer metadataCtx.Unlock()
171 >
172 > value, ok := metadataCtx.Metadata[key]
173 > return value, ok
174 }
175