...

Source file src/k8s.io/kubernetes/pkg/registry/core/service/storage/transaction_test.go

Documentation: k8s.io/kubernetes/pkg/registry/core/service/storage

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package storage
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func Test_metaTransaction(t *testing.T) {
    24  	const initial = 10
    25  	var temp int
    26  
    27  	tests := []struct {
    28  		name  string
    29  		mt    metaTransaction
    30  		start int
    31  		want  int
    32  	}{{
    33  		name: "commit and revert match",
    34  		mt: metaTransaction{
    35  			callbackTransaction{
    36  				commit: func() {
    37  					temp = temp + 1
    38  				},
    39  				revert: func() {
    40  					temp = temp - 1
    41  				},
    42  			},
    43  		},
    44  		want: 10,
    45  	}, {
    46  		name: "commit and revert match multiple times",
    47  		mt: metaTransaction{
    48  			callbackTransaction{
    49  				commit: func() {
    50  					temp = temp + 1
    51  				},
    52  				revert: func() {
    53  					temp = temp - 1
    54  				},
    55  			},
    56  			callbackTransaction{
    57  				commit: func() {
    58  					temp = temp + 2
    59  				},
    60  				revert: func() {
    61  					temp = temp - 2
    62  				},
    63  			},
    64  			callbackTransaction{
    65  				commit: func() {
    66  					temp = temp + 3
    67  				},
    68  				revert: func() {
    69  					temp = temp - 3
    70  				},
    71  			},
    72  		},
    73  		want: 10,
    74  	}, {
    75  		name: "missing revert",
    76  		mt: metaTransaction{
    77  			callbackTransaction{
    78  				commit: func() {
    79  					temp = temp + 1
    80  				},
    81  				revert: func() {
    82  					temp = temp - 1
    83  				},
    84  			},
    85  			callbackTransaction{
    86  				commit: func() {
    87  					temp = temp + 2
    88  				},
    89  			},
    90  			callbackTransaction{
    91  				commit: func() {
    92  					temp = temp + 3
    93  				},
    94  				revert: func() {
    95  					temp = temp - 3
    96  				},
    97  			},
    98  		},
    99  		want: 12,
   100  	}, {
   101  		name: "missing commit",
   102  		mt: metaTransaction{
   103  			callbackTransaction{
   104  				commit: func() {
   105  					temp = temp + 1
   106  				},
   107  				revert: func() {
   108  					temp = temp - 1
   109  				},
   110  			},
   111  			callbackTransaction{
   112  				revert: func() {
   113  					temp = temp - 2
   114  				},
   115  			},
   116  			callbackTransaction{
   117  				commit: func() {
   118  					temp = temp + 3
   119  				},
   120  				revert: func() {
   121  					temp = temp - 3
   122  				},
   123  			},
   124  		},
   125  		want: 8,
   126  	}, {
   127  		name: "commit and revert match multiple but different order",
   128  		mt: metaTransaction{
   129  			callbackTransaction{
   130  				commit: func() {
   131  					temp = temp + 1
   132  				},
   133  				revert: func() {
   134  					temp = temp - 2
   135  				},
   136  			},
   137  			callbackTransaction{
   138  				commit: func() {
   139  					temp = temp + 2
   140  				},
   141  				revert: func() {
   142  					temp = temp - 1
   143  				},
   144  			},
   145  			callbackTransaction{
   146  				commit: func() {
   147  					temp = temp + 3
   148  				},
   149  				revert: func() {
   150  					temp = temp - 3
   151  				},
   152  			},
   153  		},
   154  		want: 10,
   155  	}}
   156  	t.Parallel()
   157  	for _, tt := range tests {
   158  		t.Run(tt.name, func(t *testing.T) {
   159  			temp = initial
   160  			tt.mt.Commit()
   161  			tt.mt.Revert()
   162  			if temp != tt.want {
   163  				t.Fatalf("expected %d got %d", tt.want, temp)
   164  			}
   165  		})
   166  	}
   167  }
   168  

View as plain text