Repository navigation
Expand file tree
/
Copy pathenvironment_context_test.go
More file actions
126 lines (106 loc) · 3.37 KB
/
Copy pathenvironment_context_test.go
File metadata and controls
126 lines (106 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package iceberg
import (
"fmt"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func preserveEnvironmentProperties(t *testing.T, keys ...string) {
t.Helper()
previous := EnvironmentContext()
t.Cleanup(func() {
for _, key := range keys {
if value, ok := previous[key]; ok {
SetEnvironmentProperty(key, value)
} else {
RemoveEnvironmentProperty(key)
}
}
})
}
func TestEnvironmentContextCopiesValues(t *testing.T) {
const key = "test-engine"
preserveEnvironmentProperties(t, key)
SetEnvironmentProperty(key, "go")
context := EnvironmentContext()
context[key] = "changed"
assert.Equal(t, "go", EnvironmentContext()[key])
assert.Equal(t, fullVersion(), EnvironmentContext()[environmentContextIcebergVersionKey])
RemoveEnvironmentProperty(key)
assert.NotContains(t, EnvironmentContext(), key)
}
func TestEnvironmentContextAllowsIcebergVersionMutation(t *testing.T) {
preserveEnvironmentProperties(t, environmentContextIcebergVersionKey)
SetEnvironmentProperty(environmentContextIcebergVersionKey, "custom")
assert.Equal(t, "custom", EnvironmentContext()[environmentContextIcebergVersionKey])
RemoveEnvironmentProperty(environmentContextIcebergVersionKey)
assert.NotContains(t, EnvironmentContext(), environmentContextIcebergVersionKey)
}
func TestEnvironmentContextConcurrentAccess(t *testing.T) {
const workers = 8
const iterations = 100
keys := make([]string, 0, workers)
for worker := range workers {
keys = append(keys, fmt.Sprintf("test-concurrent-%d", worker))
}
preserveEnvironmentProperties(t, keys...)
var wg sync.WaitGroup
for worker, key := range keys {
wg.Add(1)
go func(worker int, key string) {
defer wg.Done()
for iteration := range iterations {
SetEnvironmentProperty(key, strconv.Itoa(iteration))
_ = EnvironmentContext()[key]
if (worker+iteration)%2 == 0 {
RemoveEnvironmentProperty(key)
}
}
}(worker, key)
}
wg.Wait()
}
func TestEnvironmentContextSnapshotConcurrentMutation(t *testing.T) {
const key = "test-snapshot-concurrent"
const iterations = 100
preserveEnvironmentProperties(t, key)
SetEnvironmentProperty(key, "initial")
snapshot := EnvironmentContext()
start := make(chan struct{})
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
<-start
for iteration := range iterations {
snapshot[key] = strconv.Itoa(iteration)
}
}()
go func() {
defer wg.Done()
<-start
for iteration := range iterations {
SetEnvironmentProperty(key, strconv.Itoa(iteration))
}
}()
close(start)
wg.Wait()
assert.Equal(t, strconv.Itoa(iterations-1), EnvironmentContext()[key])
}