github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/actionstack/actionstack_test.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package actionstack
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestMain(t *testing.T) {
    25  	as := NewActionStack()
    26  	var arr []int
    27  	as.Push(func() {
    28  		arr = append(arr, 1)
    29  	})
    30  	as.Push(
    31  		func() {
    32  			arr = append(arr, 2)
    33  		},
    34  		func() {
    35  			arr = append(arr, 3)
    36  		},
    37  	)
    38  
    39  	as.Run()
    40  
    41  	if len(arr) != 3 {
    42  		t.Fail()
    43  	}
    44  	for i, v := range []int{3, 2, 1} {
    45  		if arr[i] != v {
    46  			t.Fail()
    47  		}
    48  	}
    49  }
    50  
    51  func TestEmpty(t *testing.T) {
    52  	// Should at least not panic due to some nil-values
    53  	NewActionStack().Run()
    54  }
    55  
    56  func TestRunPanicsAfterRun(t *testing.T) {
    57  	var failedSuccessfully bool
    58  	as := NewActionStack()
    59  	as.Push(func() {})
    60  	as.Run()
    61  
    62  	func() {
    63  		defer func() {
    64  			if r := recover(); r != nil {
    65  				failedSuccessfully = true
    66  			}
    67  		}()
    68  		as.Run()
    69  	}()
    70  
    71  	if !failedSuccessfully {
    72  		t.Fail()
    73  	}
    74  }
    75  
    76  func TestAddPanicsAfterRun(t *testing.T) {
    77  	var failedSuccessfully bool
    78  	as := NewActionStack()
    79  	as.Run()
    80  
    81  	func() {
    82  		defer func() {
    83  			if r := recover(); r != nil {
    84  				failedSuccessfully = true
    85  			}
    86  		}()
    87  		as.Push(func() {})
    88  	}()
    89  
    90  	if !failedSuccessfully {
    91  		t.Fail()
    92  	}
    93  }