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

cache: Return IndexExistsError immediately #182

Merged
merged 1 commit into from
Jul 1, 2021
Merged
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
8 changes: 2 additions & 6 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (r *RowCache) create(uuid string, m model.Model) error {
return err
}
newIndexes := newColumnToValue(r.schema.Indexes)
var errs []error
for index := range r.indexes {

val, err := valueFromIndex(info, index)
Expand All @@ -120,14 +119,11 @@ func (r *RowCache) create(uuid string, m model.Model) error {
return err
}
if existing, ok := r.indexes[index][val]; ok {
errs = append(errs,
NewIndexExistsError(r.name, val, index, uuid, existing))
return NewIndexExistsError(r.name, val, index, uuid, existing)
}
newIndexes[index][val] = uuid
}
if len(errs) != 0 {
return fmt.Errorf("%v", errs)
}

// write indexes
for k1, v1 := range newIndexes {
for k2, v2 := range v1 {
Expand Down
17 changes: 13 additions & 4 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,40 +179,46 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {
tc, err := NewTableCache(&schema, db, testData)
require.Nil(t, err)
tests := []struct {
name string
uuid string
model *testModel
wantErr bool
name string
uuid string
model *testModel
wantErr bool
wantIndexExistsErr bool
}{
{
"inserts a new row",
"foo",
&testModel{Foo: "foo", Bar: "foo"},
false,
false,
},
{
"error duplicate uuid",
"bar",
&testModel{Foo: "bar", Bar: "bar"},
true,
false,
},
{
"error duplicate index",
"baz",
&testModel{Foo: "foo", Bar: "foo"},
true,
true,
},
{
"new row with one duplicate value",
"baz",
&testModel{Foo: "foo", Bar: "bar"},
false,
false,
},
{
"new row with other duplicate value",
"quux",
&testModel{Foo: "bar", Bar: "baz"},
false,
false,
},
}
for _, tt := range tests {
Expand All @@ -222,6 +228,9 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {
err := rc.Create(tt.uuid, tt.model)
if tt.wantErr {
assert.Error(t, err)
if tt.wantIndexExistsErr {
assert.IsType(t, &IndexExistsError{}, err)
}
} else {
assert.Nil(t, err)
mapperInfo, err := mapper.NewInfo(tSchema, tt.model)
Expand Down
55 changes: 55 additions & 0 deletions server/server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,58 @@ func TestClientServerInsertAndDelete(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, 1, reply[0].Count)
}

func TestClientServerInsertDuplicate(t *testing.T) {
defDB, err := model.NewDBModel("Open_vSwitch", map[string]model.Model{
"Open_vSwitch": &ovsType{},
"Bridge": &bridgeType{}})
require.Nil(t, err)

schema, err := getSchema()
require.Nil(t, err)

ovsDB := NewInMemoryDatabase(map[string]*model.DBModel{"Open_vSwitch": defDB})
rand.Seed(time.Now().UnixNano())
tmpfile := fmt.Sprintf("/tmp/ovsdb-%d.sock", rand.Intn(10000))
defer os.Remove(tmpfile)
server, err := NewOvsdbServer(ovsDB, DatabaseModel{
Model: defDB,
Schema: schema,
})
assert.Nil(t, err)

go func(t *testing.T, o *OvsdbServer) {
if err := o.Serve("unix", tmpfile); err != nil {
t.Error(err)
}
}(t, server)
defer server.Close()
require.Eventually(t, func() bool {
return server.Ready()
}, 1*time.Second, 10*time.Millisecond)

ovs, err := client.NewOVSDBClient(defDB, client.WithEndpoint(fmt.Sprintf("unix:%s", tmpfile)))
require.NoError(t, err)
err = ovs.Connect(context.Background())
require.NoError(t, err)

bridgeRow := &bridgeType{
Name: "foo",
ExternalIds: map[string]string{"go": "awesome", "docker": "made-for-each-other"},
}

ops, err := ovs.Create(bridgeRow)
require.Nil(t, err)
reply, err := ovs.Transact(ops...)
require.Nil(t, err)
_, err = ovsdb.CheckOperationResults(reply, ops)
require.Nil(t, err)

// duplicate
reply, err = ovs.Transact(ops...)
require.Nil(t, err)
opErrs, err := ovsdb.CheckOperationResults(reply, ops)
require.Error(t, err)
require.Error(t, opErrs[0])
require.IsTypef(t, &ovsdb.ConstraintViolation{}, opErrs[0], opErrs[0].Error())
}