github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/runtimecontextmanager_test.go (about)

     1  //go:build !noquotas
     2  // +build !noquotas
     3  
     4  package runtime
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func Test_runtimeContextManager_LinearUnused(t *testing.T) {
    11  	type fields struct {
    12  		hardLimits    RuntimeResources
    13  		usedResources RuntimeResources
    14  	}
    15  	type args struct {
    16  		cpuFactor uint64
    17  	}
    18  	tests := []struct {
    19  		name   string
    20  		fields fields
    21  		args   args
    22  		want   uint64
    23  	}{
    24  		{
    25  			name: "no limits",
    26  			args: args{cpuFactor: 10},
    27  			want: 0,
    28  		},
    29  		{
    30  			name: "no mem limit",
    31  			fields: fields{
    32  				hardLimits: RuntimeResources{Cpu: 1000},
    33  			},
    34  			args: args{cpuFactor: 5},
    35  			want: 5000,
    36  		},
    37  		{
    38  			name: "no cpu limit",
    39  			fields: fields{
    40  				hardLimits: RuntimeResources{Memory: 1000},
    41  			},
    42  			args: args{cpuFactor: 5},
    43  			want: 1000,
    44  		},
    45  		{
    46  			name: "cpu wins",
    47  			fields: fields{
    48  				hardLimits: RuntimeResources{Memory: 1000, Cpu: 100},
    49  			},
    50  			args: args{cpuFactor: 5},
    51  			want: 500,
    52  		},
    53  		{
    54  			name: "mem wins",
    55  			fields: fields{
    56  				hardLimits: RuntimeResources{Memory: 1000, Cpu: 500},
    57  			},
    58  			args: args{cpuFactor: 10},
    59  			want: 1000,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			m := &runtimeContextManager{
    65  				hardLimits:    tt.fields.hardLimits,
    66  				usedResources: tt.fields.usedResources,
    67  			}
    68  			if got := m.LinearUnused(tt.args.cpuFactor); got != tt.want {
    69  				t.Errorf("runtimeContextManager.LinearUnused() = %v, want %v", got, tt.want)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func Test_runtimeContextManager_TerminateContext(t *testing.T) {
    76  	type fields struct {
    77  		status RuntimeContextStatus
    78  	}
    79  	tests := []struct {
    80  		name      string
    81  		fields    fields
    82  		wantPanic bool
    83  	}{
    84  		{
    85  			name:      "live context",
    86  			fields:    fields{status: StatusLive},
    87  			wantPanic: true,
    88  		},
    89  		{
    90  			name:      "done context",
    91  			fields:    fields{status: StatusDone},
    92  			wantPanic: false,
    93  		},
    94  		{
    95  			name:      "error context",
    96  			fields:    fields{status: StatusError},
    97  			wantPanic: false,
    98  		},
    99  		{
   100  			name:      "killed context",
   101  			fields:    fields{status: StatusKilled},
   102  			wantPanic: false,
   103  		},
   104  	}
   105  	for _, tt := range tests {
   106  		t.Run(tt.name, func(t *testing.T) {
   107  			m := &runtimeContextManager{
   108  				status: tt.fields.status,
   109  			}
   110  			defer func() {
   111  				switch recover() {
   112  				case nil:
   113  					if tt.wantPanic {
   114  						t.Error("should panic")
   115  					}
   116  				default:
   117  					if !tt.wantPanic {
   118  						t.Error("should not panic")
   119  					} else if m.status != StatusKilled {
   120  						t.Error("context status should be StatusKilled")
   121  					}
   122  				}
   123  			}()
   124  			m.TerminateContext("error")
   125  		})
   126  	}
   127  }