Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Make GroupProfileMap Goroutine concurrent safe #318

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
.okta
.idea
.okta.yaml
.vscode/settings.json
.vscode
67 changes: 45 additions & 22 deletions okta/groupProfile.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion okta/okta.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion openapi/generator/createdFiles.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions openapi/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ function getImports(object) {

if (object.model.modelName === "GroupProfile") {
imports.push("encoding/json");
imports.push("sync");
}

return imports;
Expand Down
70 changes: 46 additions & 24 deletions openapi/generator/templates/model.go.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type {{model.modelName}}Resource resource

{{/if}}
{{#if (eq model.modelName "GroupProfile")}}
type {{model.modelName}}Map map[string]interface{}
// type {{model.modelName}}Map map[string]interface{}

{{/if}}
{{#if (eq model.modelName "ApplicationSettingsApplication")}}
Expand All @@ -52,7 +52,8 @@ type {{model.modelName}} string
type {{model.modelName}} struct {
{{{buildModelProperties model}}}
{{#if (eq model.modelName "GroupProfile")}}
{{model.modelName}}Map
// {{model.modelName}}Map
{{model.modelName}}Map sync.Map
{{/if}}
}
{{/if}}
Expand Down Expand Up @@ -100,36 +101,57 @@ func (a *{{model.modelName}}) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var profile map[string]interface{}
err := json.Unmarshal(data, &profile)
if err != nil {
return err
}
a.Name, _ = profile["name"].(string)
a.Description, _ = profile["description"].(string)
delete(profile, "name")
delete(profile, "description")
a.{{model.modelName}}Map = profile
// var profile map[string]interface{}
// err := json.Unmarshal(data, &profile)
// if err != nil {
// return err
// }
// a.Name, _ = profile["name"].(string)
// a.Description, _ = profile["description"].(string)
// delete(profile, "name")
// delete(profile, "description")
// a.{{model.modelName}}Map = profile

var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
return err
}
a.Name, _ = m["name"].(string)
a.Description, _ = m["description"].(string)
for key, value := range m {
a.{{model.modelName}}Map.Store(key, value)
}
a.{{model.modelName}}Map.Delete("name")
a.{{model.modelName}}Map.Delete("description")

return nil
}

func (a {{model.modelName}}) MarshalJSON() ([]byte, error) {
if len(a.{{model.modelName}}Map) == 0 {
return json.Marshal(&struct {
Name string `json:"name"`
Description string `json:"description"`
}{
Name: a.Name,
Description: a.Description,
})
}
// if len(a.{{model.modelName}}Map) == 0 {
// return json.Marshal(&struct {
// Name string `json:"name"`
// Description string `json:"description"`
// }{
// Name: a.Name,
// Description: a.Description,
// })
// }

m := make(map[string]interface{})
if a.Name != "" {
a.{{model.modelName}}Map["name"] = a.Name
//a.{{model.modelName}}Map["name"] = a.Name
m["name"] = a.Name
}
if a.Description != "" {
a.{{model.modelName}}Map["description"] = a.Description
//a.{{model.modelName}}Map["description"] = a.Description
m["description"] = a.Description
}
return json.Marshal(a.{{model.modelName}}Map)
a.{{model.modelName}}Map.Range(func(k, v interface{}) bool {
m[k.(string)] = v
return true
})
return json.Marshal(m)
}

{{/if}}
Expand Down
54 changes: 54 additions & 0 deletions tests/integration/group_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2018 - Present Okta, Inc.
*
* 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 integration

import (
"sync"
"testing"
"time"

"github.com/okta/okta-sdk-golang/v2/okta"
"github.com/stretchr/testify/assert"
)

// TestGroupProfileGoroutineSafe relates to Issue 296
// https://github.com/okta/okta-sdk-golang/issues/296
func TestGroupProfileGoroutineSafe(t *testing.T) {
gp := &okta.GroupProfile{
Name: testName("SDK_TEST Get Test Group"),
// GroupProfileMap: map[string]interface{}{},
}

var wg sync.WaitGroup
for i := 0; i < 500; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, key, value string, profile *okta.GroupProfile) {
// NOTE: before GroupProfileMap was made a sync.Map this loop
// calling many go routines would cause a "fatal error: concurrent
// map writes" panic as GroupProfileMap was a golang map.

// profile.GroupProfileMap[kjey] = name

profile.GroupProfileMap.Store(key, value)
time.Sleep(time.Duration(time.Millisecond))
wg.Done()
}(&wg, "example", randomTestString(), gp)
}
_, found := gp.GroupProfileMap.Load("example")
assert.True(t, found, "example value not found in GroupProfileMap")
wg.Wait()
}
34 changes: 23 additions & 11 deletions tests/integration/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,19 @@ func TestGroupProfileSerialization(t *testing.T) {
gp := okta.GroupProfile{
Name: "test",
Description: "tester",
GroupProfileMap: okta.GroupProfileMap{
"custom": "value",
},
//GroupProfileMap: okta.GroupProfileMap{
// "custom": "value",
//},
}
gp.GroupProfileMap.Store("example", "test value")

gpExpected := okta.GroupProfile{
Name: "test",
Description: "tester",
GroupProfileMap: okta.GroupProfileMap{
"custom": "value",
},
}
//gpExpected := okta.GroupProfile{
// Name: "test",
// Description: "tester",
// //GroupProfileMap: okta.GroupProfileMap{
// // "custom": "value",
// //},
//}

b, err := json.Marshal(&gp)
require.NoError(t, err)
Expand All @@ -412,7 +413,18 @@ func TestGroupProfileSerialization(t *testing.T) {
err = json.Unmarshal(b, &gpCopy)
require.NoError(t, err)

assert.Equal(t, gpExpected, gpCopy, "expected marshal to unmarshal to produce exact copy of group profile")
// assert.Equal(t, gpExpected, gpCopy, "expected marshal to unmarshal to produce exact copy of group profile")
assert.Equal(t, "test", gpCopy.Name)
assert.Equal(t, "tester", gpCopy.Description)
value, found := gpCopy.GroupProfileMap.Load("example")
assert.True(t, found)
assert.Equal(t, value, "test value")
countedValues := 0
gpCopy.GroupProfileMap.Range(func(k, v interface{}) bool {
countedValues++
return true
})
assert.Equal(t, 1, countedValues)
}

func TestListAssignedApplicationsForGroup(t *testing.T) {
Expand Down