Atlas › Test

TestOnceMap

Exact test identity: go.temporal.io/server/common/collection/TestOnceMap

Package
go.temporal.io/server/common/collection
Suite / test hierarchy
TestOnceMap
Test
TestOnceMap
Introduced at
oncemap.go ×2 Frontier kind: Joint frontier
Covered ranges
3
Covered lines
18
Covered files
1

Covered source

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

go.temporal.io/server/common/collection/oncemap.go 18 covered LOC · 3 ranges

Open complete file

13 // NewOnceMap creates a [OnceMap] from a given construct function.
14 // construct should be kept light as it is called while holding a lock on the entire map.
15 > func NewOnceMap[K comparable, T any](construct func(K) T) *OnceMap[K, T] { oncemap.go
16 > return &OnceMap[K, T]{
17 > construct: construct,
18 > inner: make(map[K]T, 0),
19 > }
20 > }
21
22 > func (m *OnceMap[K, T]) Get(key K) T { oncemap.go
23 > m.mu.RLock()
24 > value, ok := m.inner[key]
25 > m.mu.RUnlock()
26 > if !ok {
27 > m.mu.Lock()
28 > defer m.mu.Unlock()
29 > if value, ok = m.inner[key]; !ok {
30 > value = m.construct(key)
31 > m.inner[key] = value
32 > }
33 }
34
35 > return value oncemap.go
36 }
37