Skip to content

Commit

Permalink
Init function bypass Set when no ECS Environment detected (#11)
Browse files Browse the repository at this point in the history
* ignore set when no ecs env detected

* refactor agent code for test [temp]

* refactor test maxprocs

* temp refactor task

* temp refactor of task

* remove method from test

* move refactored agent into task

* temp refactor of task

* refactored all tests with tasktest ecs agent

* fix lint warnings

* refactored all tests to use agent

* remove unused parameter from ecs agent

* update go docs

* fix lint warning

---------

Co-authored-by: rf <[email protected]>
  • Loading branch information
rdforte and rf authored Dec 27, 2024
1 parent 339c93c commit fed4c8e
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 261 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ issues:
- path: maxprocs/maxprocs_test.go
linters:
- paralleltest # disable paralleltest for testing GOMAXPROCS env variable.
- path: gomaxecs_test.go
linters:
- paralleltest # disable paralleltest for testing GOMAXPROCS env variable.

linters-settings:
depguard:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: cover
cover:
go test -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./...
go test --race -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./...
go tool cover -html=cover.out -o cover.html

.PHONY: lint
Expand Down
10 changes: 9 additions & 1 deletion gomaxecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ import (
)

func init() {
_, _ = maxprocs.Set(maxprocs.WithLogger(log.Printf))
runSetMaxProcs()
}

func runSetMaxProcs() {
if maxprocs.IsECS() {
_, _ = maxprocs.Set(maxprocs.WithLogger(log.Printf))
} else {
log.Printf("gomaxecs: ECS environment not detected. Skipping set GOMAXPROCS")
}
}
45 changes: 42 additions & 3 deletions gomaxecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,46 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Validate init function builds as expected.
package gomaxecs_test
// NOTE: This file is intentionally testing a private function.
// This is to ensure that the function runSetMaxProcs is tested
// and remains private to the package.

import _ "github.com/rdforte/gomaxecs"
package gomaxecs //nolint:testpackage // Test private function.

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"

"github.com/rdforte/gomaxecs/internal/task/tasktest"
)

func TestGomaxecs_runSetMaxProcs_ECSEnvNotDetected(t *testing.T) {
t.Parallel()

curMaxProcs := runtime.GOMAXPROCS(0)

runSetMaxProcs()

assert.Equal(t, curMaxProcs, runtime.GOMAXPROCS(0))
}

func TestGomaxecs_runSetMaxProcs_ECSEnvDetected(t *testing.T) {
curMaxProcs := 1
runtime.GOMAXPROCS(curMaxProcs)

wantCPUs := 2
containerCPU, taskCPU := wantCPUs<<10, wantCPUs

agent := tasktest.NewECSAgent(t).
WithContainerMetaEndpoint(containerCPU).
WithTaskMetaEndpoint(containerCPU, taskCPU).
Start().
SetMetaURIEnv()
defer agent.Close()

runSetMaxProcs()

assert.Equal(t, wantCPUs, runtime.GOMAXPROCS(0))
}
Loading

0 comments on commit fed4c8e

Please sign in to comment.