forked from kubernetes-sigs/kube-scheduler-wasm-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes-sigs#66 from codefromthecrypt/simplify-…
…host guest: inlines host.GetConfig to config.Get
- Loading branch information
Showing
10 changed files
with
128 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed 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 internal allows unit testing without requiring wasm imports. | ||
package internal | ||
|
||
import "unsafe" | ||
|
||
var ( | ||
// config is lazy read on Get. | ||
config string | ||
// configRead is required to differentiate an empty read from never read. | ||
configRead bool | ||
) | ||
|
||
// Get lazy reads configuration from the given function and returns a byte | ||
// slice of the result. | ||
func Get(readConfig func() string) []byte { | ||
if !configRead { | ||
config = readConfig() | ||
configRead = true | ||
} | ||
if config == "" { | ||
return nil // don't call unsafe.StringData("") | ||
} | ||
// Return the bytes under `config`. This is safe because `config` is | ||
// package-scoped, so is always kept alive. This is an alternative to | ||
// maintaining `mem.GetBytes` or `[]byte(stringConfig)`, which allocates. | ||
return unsafe.Slice(unsafe.StringData(config), len(config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed 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 internal | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected []byte | ||
}{ | ||
{name: "empty"}, | ||
{ | ||
name: "NUL terminated", | ||
input: string([]byte{'a', 0, 't', 'w', 'o', 0}), | ||
expected: []byte{'a', 0, 't', 'w', 'o', 0}, | ||
}, | ||
{ | ||
name: "unicode", | ||
input: "fóo", | ||
expected: []byte{'f', 0xc3, 0xb3, 'o'}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
configRead = false | ||
defer func() { | ||
configRead = false | ||
}() | ||
|
||
for _, fn := range []func() string{ | ||
func() string { | ||
return tc.input | ||
}, | ||
func() string { | ||
panic("should cache the first read") | ||
}, | ||
} { | ||
if want, have := tc.expected, Get(fn); (want == nil && have != nil) || !bytes.Equal(want, have) { | ||
t.Fatalf("unexpected config: %v != %v", want, have) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters