github.com/dim4egster/coreth@v0.10.2/plugin/evm/config_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestUnmarshalConfig(t *testing.T) {
    16  	tests := []struct {
    17  		name        string
    18  		givenJSON   []byte
    19  		expected    Config
    20  		expectedErr bool
    21  	}{
    22  		{
    23  			"string durations parsed",
    24  			[]byte(`{"api-max-duration": "1m", "continuous-profiler-frequency": "2m"}`),
    25  			Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}},
    26  			false,
    27  		},
    28  		{
    29  			"integer durations parsed",
    30  			[]byte(fmt.Sprintf(`{"api-max-duration": "%v", "continuous-profiler-frequency": "%v"}`, 1*time.Minute, 2*time.Minute)),
    31  			Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}},
    32  			false,
    33  		},
    34  		{
    35  			"nanosecond durations parsed",
    36  			[]byte(`{"api-max-duration": 5000000000, "continuous-profiler-frequency": 5000000000}`),
    37  			Config{APIMaxDuration: Duration{5 * time.Second}, ContinuousProfilerFrequency: Duration{5 * time.Second}},
    38  			false,
    39  		},
    40  		{
    41  			"bad durations",
    42  			[]byte(`{"api-max-duration": "bad-duration"}`),
    43  			Config{},
    44  			true,
    45  		},
    46  
    47  		{
    48  			"state sync enabled",
    49  			[]byte(`{"state-sync-enabled":true}`),
    50  			Config{StateSyncEnabled: true},
    51  			false,
    52  		},
    53  		{
    54  			"state sync sources",
    55  			[]byte(`{"state-sync-ids": "NodeID-CaBYJ9kzHvrQFiYWowMkJGAQKGMJqZoat"}`),
    56  			Config{StateSyncIDs: "NodeID-CaBYJ9kzHvrQFiYWowMkJGAQKGMJqZoat"},
    57  			false,
    58  		},
    59  	}
    60  
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			var tmp Config
    64  			err := json.Unmarshal(tt.givenJSON, &tmp)
    65  			if tt.expectedErr {
    66  				assert.Error(t, err)
    67  			} else {
    68  				assert.NoError(t, err)
    69  				assert.Equal(t, tt.expected, tmp)
    70  			}
    71  		})
    72  	}
    73  }