Skip to content

Commit

Permalink
🐛 Fix concurrent tag/category Ensure(). (konveyor#629)
Browse files Browse the repository at this point in the history
_Look-and-See_ logic replaced to support concurrent operations.

---------

Signed-off-by: Jeff Ortel <jortel@redhat.com>
  • Loading branch information
jortel authored Apr 25, 2024
1 parent cdc096f commit ae3cb7d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
22 changes: 15 additions & 7 deletions binding/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package binding

import (
"errors"

"github.com/konveyor/tackle2-hub/api"
)

Expand Down Expand Up @@ -66,14 +68,20 @@ func (h *Tag) Find(name string, category uint) (r *api.Tag, found bool, err erro

// Ensure a tag exists.
func (h *Tag) Ensure(wanted *api.Tag) (err error) {
tag, found, err := h.Find(wanted.Name, wanted.Category.ID)
if err != nil {
return
}
if !found {
for i := 0; i < 10; i++ {
err = h.Create(wanted)
} else {
*wanted = *tag
if err == nil {
return
}
found := false
if errors.Is(err, &Conflict{}) {
var tag *api.Tag
tag, found, err = h.Find(wanted.Name, wanted.Category.ID)
if found {
*wanted = *tag
break
}
}
}
return
}
22 changes: 15 additions & 7 deletions binding/tagcategory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package binding

import (
"errors"

"github.com/konveyor/tackle2-hub/api"
)

Expand Down Expand Up @@ -65,14 +67,20 @@ func (h *TagCategory) Find(name string) (r *api.TagCategory, found bool, err err

// Ensure a tag-type exists.
func (h *TagCategory) Ensure(wanted *api.TagCategory) (err error) {
tp, found, err := h.Find(wanted.Name)
if err != nil {
return
}
if !found {
for i := 0; i < 10; i++ {
err = h.Create(wanted)
} else {
*wanted = *tp
if err == nil {
return
}
found := false
if errors.Is(err, &Conflict{}) {
var cat *api.TagCategory
cat, found, err = h.Find(wanted.Name)
if found {
*wanted = *cat
break
}
}
}
return
}
24 changes: 22 additions & 2 deletions test/api/tag/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
func TestTagCRUD(t *testing.T) {
for _, r := range Samples {
t.Run(r.Name, func(t *testing.T) {
var err error
// Create.
err := Tag.Create(&r)
err = Tag.Create(&r)
if err != nil {
t.Errorf(err.Error())
}
Expand Down Expand Up @@ -44,11 +45,30 @@ func TestTagCRUD(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}

_, err = Tag.Get(r.ID)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", r)
}
// Ensure.
r.ID = 0
err = Tag.Ensure(&r)
if err != nil {
t.Errorf(err.Error())
}
got, err = Tag.Get(r.ID)
if err != nil {
t.Errorf(err.Error())
}
if got.ID == 0 {
t.Errorf("Ensured resource has no id.")
}
if got.Name != r.Name {
t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name)
}
err = Tag.Delete(r.ID)
if err != nil {
t.Errorf(err.Error())
}
})
}
}
Expand Down
25 changes: 23 additions & 2 deletions test/api/tagcategory/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
func TestTagCategoryCRUD(t *testing.T) {
for _, r := range Samples {
t.Run(r.Name, func(t *testing.T) {
var err error
// Create.
err := TagCategory.Create(&r)
err = TagCategory.Create(&r)
if err != nil {
t.Errorf(err.Error())
}
Expand Down Expand Up @@ -44,11 +45,31 @@ func TestTagCategoryCRUD(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}

_, err = TagCategory.Get(r.ID)
if err == nil {
t.Errorf("Resource exits, but should be deleted: %v", r)
}

// Ensure.
r.ID = 0
err = TagCategory.Ensure(&r)
if err != nil {
t.Errorf(err.Error())
}
got, err = TagCategory.Get(r.ID)
if err != nil {
t.Errorf(err.Error())
}
if got.ID == 0 {
t.Errorf("Ensured resource has no id.")
}
if got.Name != r.Name {
t.Errorf("Different response error. Got %s, expected %s", got.Name, r.Name)
}
err = TagCategory.Delete(r.ID)
if err != nil {
t.Errorf(err.Error())
}
})
}
}
Expand Down

0 comments on commit ae3cb7d

Please sign in to comment.