Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cache.sh « test - github.com/freebsd/poudriere.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 34e9aa94d8d051e7c8e6afc83a8cfbe11677aa1b (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#! /bin/sh

. $(realpath $(dirname $0))/common.sh
. ${SCRIPTPREFIX}/include/util.sh
. ${SCRIPTPREFIX}/include/hash.sh
. ${SCRIPTPREFIX}/include/shared_hash.sh
. ${SCRIPTPREFIX}/include/cache.sh

USE_CACHE_CALL=1

MASTERMNT=$(mktemp -d)

_lookup_key() {
	[ $# -ge 2 ] || eargs _lookup_key var_return func [args]
	local var_return="$1"
	local func="$2"
	shift 2
	local encoded

	encode_args encoded "$@"
	setvar "${var_return}" "${func}-${encoded}"
}

real_func() {
	msg_warn "in real_func $# $@"
	local lookup lookup_key

	_lookup_key lookup_key "real_func" "$@"
	shash_get lookupcnt "${lookup_key}" lookup || lookup=0
	lookup=$((lookup + 1))
	shash_set lookupcnt "${lookup_key}" ${lookup}

	echo "$# $@"
}

real_func_sv() {
	msg_warn "in real_func_sv $# $@"
	local var_return="$1"
	shift
	local lookup lookup_key

	_lookup_key lookup_key "real_func_sv" "$@"
	shash_get lookupcnt "${lookup_key}" lookup || lookup=0
	lookup=$((lookup + 1))
	shash_set lookupcnt "${lookup_key}" ${lookup}

	setvar "${var_return}" "$# $@"
}

real_func_sv_2() {
	msg_warn "in real_func_sv_2 $# $@"
	local data="$1"
	local var_return="$2"
	local lookup lookup_key

	_lookup_key lookup_key "real_func_sv_2" "${data}"
	shash_get lookupcnt "${lookup_key}" lookup || lookup=0
	lookup=$((lookup + 1))
	shash_set lookupcnt "${lookup_key}" ${lookup}

	setvar "${var_return}" "1 ${data}"
}

get_lookup_cnt() {
	[ $# -ge 2 ] || eargs get_lookup_cnt var_return func [args]
	local var_return="$1"
	local func="$2"
	shift 2
	local key _lookup

	_lookup_key key "${func}" "$@"
	shash_get lookupcnt "${key}" _lookup || _lookup=0
	setvar "${var_return}" "${_lookup}"
}

echo "Working on ${MASTERMNT}"
SHASH_VAR_PATH="${MASTERMNT}"

# Simple test with 1 argument
{
	# First lookup, will call into the real function
	lookup=0
	value=
	cache_call value real_func "1"
	assert 0 $? "real_func 1 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 1 argcnt"
	assert "1" "${value}" "real_func 1 value"
	get_lookup_cnt lookup real_func "1"
	assert 0 $? "lookupcnt real_func-1"
	assert 1 ${lookup} "real_func 1 lookup count"

	# Second lookup, should not call into the function
	value=
	cache_call value real_func "1"
	assert 0 $? "real_func 1 return status 2"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 1 argcnt 2"
	assert "1" "${value}" "real_func 1 value 2"
	get_lookup_cnt lookup real_func "1"
	assert 0 $? "lookupcnt real_func-1 2"
	assert 1 ${lookup} "real_func 1 lookup count 2"
}

# More complex argument test
{
	# First lookup, will call into the real function
	lookup=0
	get_lookup_cnt lookup real_func "1" "2.0" "3 4"
	value=
	cache_call value real_func "1" "2.0" "3 4"
	assert 0 $? "real_func 1 2.0 '3 4' return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 3 "${argcnt}" "real_func 1 2.0 '3 4' argcnt"
	assert "1 2.0 3 4" "${value}" "real_func 1 2.0 '3 4' value"
	get_lookup_cnt lookup real_func "1" "2.0" "3 4"
	assert 0 $? "lookupcnt real_func-1 2.0 '3 4'"
	assert 1 ${lookup} "real_func 1 2.0 '3 4' lookup count"

	# Second lookup, should not call into the function
	value=
	cache_call value real_func "1" "2.0" "3 4"
	assert 0 $? "real_func 1 2.0 '3 4' return status 2"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 3 "${argcnt}" "real_func 1 2.0 '3 4' argcnt 2"
	assert "1 2.0 3 4" "${value}" "real_func 1 2.0 '3 4' value 2"
	get_lookup_cnt lookup real_func "1" "2.0" "3 4"
	assert 0 $? "lookupcnt real_func-1 2.0 '3 4' 2"
	assert 1 ${lookup} "real_func 1 2.0 '3 4' lookup count 2"

	# Manually call the function with the first data to force the
	# counter up for later tests to ensure the new data find count 0
	# and not 1 (as already cached).
	real_func "1" "2.0" "3 4" >/dev/null
	assert 0 $? "actual real_func 1 2.0 '3 4' return status"
	get_lookup_cnt lookup real_func "1" "2.0" "3 4"
	assert 2 ${lookup} "actual real_func 1 2.0 '3 4' lookup count"

	# Third lookup with trailing empty argument
	lookup=0
	value=
	cache_call value real_func "1" "2.0" "3" "4" ""
	assert 0 $? "real_func 1 2.0 3 4 _ return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 5 "${argcnt}" "real_func 1 2.0 3 4 _ argcnt"
	assert "1 2.0 3 4 " "${value}" "real_func 1 2.0 3 4 _ value"
	get_lookup_cnt lookup real_func "1" "2.0" "3" "4" ""
	assert 0 $? "lookupcnt real_func-1 2.0 3 4 _"
	assert 1 ${lookup} "real_func 1 2.0 3 4 _ lookup count"

	# Fouth lookup with similar data as first but last is split into 2,
	# should be unique.

	lookup=0
	value=
	cache_call value real_func "1" "2.0" "3" "4"
	assert 0 $? "real_func 1 2.0 3 4 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 4 "${argcnt}" "real_func 1 2.0 3 4 argcnt"
	assert "1 2.0 3 4" "${value}" "real_func 1 2.0 3 4 value"
	get_lookup_cnt lookup real_func "1" "2.0" "3" "4"
	assert 0 $? "lookupcnt real_func-1 2.0 3 4"
	assert 1 ${lookup} "real_func 1 2.0 3 4 lookup count"

	# Fifth lookup with similar data as the last

	lookup=0
	value=
	cache_call value real_func "1" "2.0" "3 " "4"
	assert 0 $? "real_func 1 2.0 3_ 4 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 4 "${argcnt}" "real_func 1 2.0 3_ 4 argcnt"
	assert "1 2.0 3  4" "${value}" "real_func 1 2.0 3_ 4 value"
	get_lookup_cnt lookup real_func "1" "2.0" "3 " "4"
	assert 0 $? "lookupcnt real_func-1 2.0 3_ 4"
	assert 1 ${lookup} "real_func 1 2.0 3_ 4 lookup count"
}

# Test a lookup with a function with uses setvar rather than stdout for results.
{
	# First lookup, will call into the real function
	lookup=0
	value=
	cache_call_sv value real_func_sv sv_value "1"
	assert 0 $? "real_func_sv 1 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func_sv 1 argcnt"
	assert "1" "${value}" "real_func_sv 1 value"
	get_lookup_cnt lookup real_func_sv "1"
	assert 0 $? "lookupcnt real_func_sv-1"
	assert 1 ${lookup} "real_func_sv 1 lookup count"

	# Second lookup, should not call into the function
	value=
	cache_call_sv value real_func_sv sv_value "1"
	assert 0 $? "real_func_sv 1 return status 2"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func_sv 1 argcnt 2"
	assert "1" "${value}" "real_func_sv 1 value 2"
	get_lookup_cnt lookup real_func_sv "1"
	assert 0 $? "lookupcnt real_func_sv-1 2"
	assert 1 ${lookup} "real_func_sv 1 lookup count 2"
}

# Test a lookup with a function with uses setvar rather than stdout for results,
# but with return var not in first place.
{
	# First lookup, will call into the real function
	lookup=0
	value=
	cache_call_sv value real_func_sv_2 "1" sv_value
	assert 0 $? "real_func_sv_2 1 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func_sv_2 1 argcnt"
	assert "1" "${value}" "real_func_sv_2 1 value"
	get_lookup_cnt lookup real_func_sv_2 "1"
	assert 0 $? "lookupcnt real_func_sv_2-1"
	assert 1 ${lookup} "real_func_sv_2 1 lookup count"

	# Second lookup, should not call into the function
	value=
	cache_call_sv value real_func_sv_2 "1" sv_value
	assert 0 $? "real_func_sv_2 1 return status 2"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func_sv_2 1 argcnt 2"
	assert "1" "${value}" "real_func_sv_2 1 value 2"
	get_lookup_cnt lookup real_func_sv_2 "1"
	assert 0 $? "lookupcnt real_func_sv_2-1 2"
	assert 1 ${lookup} "real_func_sv_2 1 lookup count 2"
}

# Invalidation test
{
	# First lookup, will call into the real function
	lookup=0
	value=
	cache_call value real_func "1"
	assert 0 $? "real_func 1 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 1 argcnt"
	assert "1" "${value}" "real_func 1 value"
	get_lookup_cnt lookup real_func "1"
	assert 0 $? "lookupcnt real_func-1"
	assert 1 ${lookup} "real_func 1 lookup count"

	# now invalidate the cache and ensure it is looked up again.
	cache_invalidate real_func "1"

	cache_call value real_func "1"
	assert 0 $? "real_func 1 return status - invalidated"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 1 argcnt - invalidated"
	assert "1" "${value}" "real_func 1 value - invalidated"
	get_lookup_cnt lookup real_func "1"
	assert 0 $? "lookupcnt real_func-1 - invalidated"
	assert 2 ${lookup} "real_func 1 lookup count - invalidated"
}

# Forced cached set test
{
	# First lookup, will call into the real function
	lookup=0
	value=
	cache_call value real_func "5"
	assert 0 $? "real_func 5 return status"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 5 argcnt"
	assert "5" "${value}" "real_func 5 value"
	get_lookup_cnt lookup real_func "5"
	assert 0 $? "lookupcnt real_func-5"
	assert 1 ${lookup} "real_func 5 lookup count"

	# now change the value in the cache ("1 " is due to real_func
	# adding in its $#)
	cache_set "1 SET-5-SET" real_func "5"

	cache_call value real_func "5"
	assert 0 $? "real_func 5 return status - set"
	argcnt=${value%% *}
	value="${value#[0-9] }"
	assert 1 "${argcnt}" "real_func 5 argcnt - set"
	assert "SET-5-SET" "${value}" "real_func 5 value - set"
	get_lookup_cnt lookup real_func "5"
	assert 0 $? "lookupcnt real_func-5 - set"
	# Should not have called the real func
	assert 1 ${lookup} "real_func 5 lookup count - set"
}


rm -rf "${MASTERMNT}"
exit 0