k8s.io/kubernetes@v1.29.3/test/e2e/chaosmonkey/chaosmonkey_test.go (about)

     1  /*
     2  Copyright 2016 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 chaosmonkey
    18  
    19  import (
    20  	"context"
    21  	"sync/atomic"
    22  	"testing"
    23  )
    24  
    25  func TestDoWithPanic(t *testing.T) {
    26  	var counter int64
    27  	cm := New(func(ctx context.Context) {})
    28  	tests := []Test{
    29  		// No panic
    30  		func(ctx context.Context, sem *Semaphore) {
    31  			defer atomic.AddInt64(&counter, 1)
    32  			sem.Ready()
    33  		},
    34  		// Panic after sem.Ready()
    35  		func(ctx context.Context, sem *Semaphore) {
    36  			defer atomic.AddInt64(&counter, 1)
    37  			sem.Ready()
    38  			panic("Panic after calling sem.Ready()")
    39  		},
    40  		// Panic before sem.Ready()
    41  		func(ctx context.Context, sem *Semaphore) {
    42  			defer atomic.AddInt64(&counter, 1)
    43  			panic("Panic before calling sem.Ready()")
    44  		},
    45  	}
    46  	for _, test := range tests {
    47  		cm.Register(test)
    48  	}
    49  	cm.Do(context.Background())
    50  	// Check that all funcs in tests were called.
    51  	if int(counter) != len(tests) {
    52  		t.Errorf("Expected counter to be %v, but it was %v", len(tests), counter)
    53  	}
    54  }