mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Svc REST: Add a transaction API
This will be used in upcoming commits, but for easier history and review it is pretty stand-alone.
This commit is contained in:
parent
14d0571a5f
commit
960b36b124
62
pkg/registry/core/service/storage/transaction.go
Normal file
62
pkg/registry/core/service/storage/transaction.go
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
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 storage
|
||||
|
||||
// transaction represents something that may need to be finalized on success or
|
||||
// failure of the larger transaction.
|
||||
type transaction interface {
|
||||
// Commit tells the transaction to finalize any changes it may have
|
||||
// pending. This cannot fail, so errors must be handled internally.
|
||||
Commit()
|
||||
|
||||
// Revert tells the transaction to abandon or undo any changes it may have
|
||||
// pending. This cannot fail, so errors must be handled internally.
|
||||
Revert()
|
||||
}
|
||||
|
||||
// metaTransaction is a collection of transactions.
|
||||
type metaTransaction []transaction
|
||||
|
||||
func (mt metaTransaction) Commit() {
|
||||
for _, t := range mt {
|
||||
t.Commit()
|
||||
}
|
||||
}
|
||||
|
||||
func (mt metaTransaction) Revert() {
|
||||
for _, t := range mt {
|
||||
t.Revert()
|
||||
}
|
||||
}
|
||||
|
||||
// callbackTransaction is a transaction which calls arbitrary functions.
|
||||
type callbackTransaction struct {
|
||||
commit func()
|
||||
revert func()
|
||||
}
|
||||
|
||||
func (cb callbackTransaction) Commit() {
|
||||
if cb.commit != nil {
|
||||
cb.commit()
|
||||
}
|
||||
}
|
||||
|
||||
func (cb callbackTransaction) Revert() {
|
||||
if cb.revert != nil {
|
||||
cb.revert()
|
||||
}
|
||||
}
|
167
pkg/registry/core/service/storage/transaction_test.go
Normal file
167
pkg/registry/core/service/storage/transaction_test.go
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
Copyright 2021 The Kubernetes Authors.
|
||||
|
||||
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 storage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_metaTransaction(t *testing.T) {
|
||||
const initial = 10
|
||||
var temp int
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mt metaTransaction
|
||||
start int
|
||||
want int
|
||||
}{{
|
||||
name: "commit and revert match",
|
||||
mt: metaTransaction{
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 1
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 1
|
||||
},
|
||||
},
|
||||
},
|
||||
want: 10,
|
||||
}, {
|
||||
name: "commit and revert match multiple times",
|
||||
mt: metaTransaction{
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 1
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 1
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 2
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 2
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 3
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 3
|
||||
},
|
||||
},
|
||||
},
|
||||
want: 10,
|
||||
}, {
|
||||
name: "missing revert",
|
||||
mt: metaTransaction{
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 1
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 1
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 2
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 3
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 3
|
||||
},
|
||||
},
|
||||
},
|
||||
want: 12,
|
||||
}, {
|
||||
name: "missing commit",
|
||||
mt: metaTransaction{
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 1
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 1
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
revert: func() {
|
||||
temp = temp - 2
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 3
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 3
|
||||
},
|
||||
},
|
||||
},
|
||||
want: 8,
|
||||
}, {
|
||||
name: "commit and revert match multiple but different order",
|
||||
mt: metaTransaction{
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 1
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 2
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 2
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 1
|
||||
},
|
||||
},
|
||||
callbackTransaction{
|
||||
commit: func() {
|
||||
temp = temp + 3
|
||||
},
|
||||
revert: func() {
|
||||
temp = temp - 3
|
||||
},
|
||||
},
|
||||
},
|
||||
want: 10,
|
||||
}}
|
||||
t.Parallel()
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
temp = initial
|
||||
tt.mt.Commit()
|
||||
tt.mt.Revert()
|
||||
if temp != tt.want {
|
||||
t.Fatalf("expected %d got %d", tt.want, temp)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user