Skip to content

Commit a525b92

Browse files
committed
Adds first-write conformance test
1 parent 1ded95f commit a525b92

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

state/azure/tablestorage/tablestorage.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,6 @@ func (r *StateStore) Set(req *state.SetRequest) error {
141141
r.logger.Debugf("saving %s", req.Key)
142142

143143
err := r.writeRow(req)
144-
if err != nil {
145-
if req.ETag != nil {
146-
return state.NewETagError(state.ETagMismatch, err)
147-
}
148-
}
149144

150145
return err
151146
}
@@ -198,20 +193,25 @@ func (r *StateStore) writeRow(req *state.SetRequest) error {
198193
}
199194
entity.OdataEtag = etag
200195

201-
// InsertOrReplace does not support ETag concurrency, therefore we will try to use Update method first
202-
// as it's more frequent, and then Insert
196+
// InsertOrReplace does not support ETag concurrency, therefore we will use Insert to check for key existence
197+
// and then use Update to update the key if it exists with the specified ETag
203198

204-
err := entity.Update(false, nil)
199+
err := entity.Insert(storage.FullMetadata, nil)
205200
if err != nil {
206-
if isNotFoundError(err) {
207-
// When entity is not found (set state first time) create it
208-
entity.OdataEtag = ""
209-
210-
return entity.Insert(storage.FullMetadata, nil)
201+
if etag == "" {
202+
return state.NewETagError(state.ETagMismatch, err)
203+
}
204+
err := entity.Update(false, nil)
205+
if err != nil {
206+
if isNotFoundError(err) {
207+
return state.NewETagError(state.ETagMismatch, err)
208+
}
211209
}
210+
211+
return err
212212
}
213213

214-
return err
214+
return nil
215215
}
216216

217217
func isNotFoundError(err error) bool {
@@ -233,9 +233,13 @@ func (r *StateStore) deleteRow(req *state.DeleteRequest) error {
233233
if req.ETag != nil {
234234
entity.OdataEtag = *req.ETag
235235

236+
// force=false sets the "If-Match: <ETag>" header to ensure that the delete is only performed if the
237+
// entity's ETag matches the specified ETag
236238
return entity.Delete(false, nil)
237239
}
238240

241+
// force=true sets the "If-Match: *" header to ensure that we delete a matching entity
242+
// regardless of the entity's ETag value
239243
return entity.Delete(true, nil)
240244
}
241245

tests/config/state/tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ components:
1212
- component: mysql
1313
operations: [ "set", "get", "delete", "bulkset", "bulkdelete", "transaction", "etag" ]
1414
- component: azure.tablestorage
15-
operations: ["set", "get", "delete", "etag", "bulkset", "bulkdelete"]
15+
operations: ["set", "get", "delete", "etag", "bulkset", "bulkdelete", "first-write"]
16+

0 commit comments

Comments
 (0)