Skip to content

Commit

Permalink
Merge pull request #5102 from algolucky/relbeta3.14.2
Browse files Browse the repository at this point in the history
  • Loading branch information
algojohnlee authored Feb 2, 2023
2 parents 39ea2c6 + 3355ce6 commit 90e744c
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 63 deletions.
2 changes: 1 addition & 1 deletion buildnumber.dat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1
2
154 changes: 106 additions & 48 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ var defaultConfig = Local{
BaseLoggerDebugLevel: 1, //Info level
}

func TestSaveThenLoad(t *testing.T) {
func TestLocal_SaveThenLoad(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

c1, err := loadWithoutDefaults(defaultConfig)
require.NoError(t, err)
Expand All @@ -53,37 +54,34 @@ func TestSaveThenLoad(t *testing.T) {
ser1 := json.NewEncoder(&b1)
ser1.Encode(c1)

os.RemoveAll("testdir")
err = os.Mkdir("testdir", 0777)
require.NoError(t, err)

c1.SaveToDisk("testdir")
tempDir := t.TempDir()
c1.SaveToDisk(tempDir)

c2, err := LoadConfigFromDisk("testdir")
c2, err := LoadConfigFromDisk(tempDir)
require.NoError(t, err)

var b2 bytes.Buffer
ser2 := json.NewEncoder(&b2)
ser2.Encode(c2)

require.True(t, bytes.Equal(b1.Bytes(), b2.Bytes()))

os.RemoveAll("testdir")
}

func TestLoadMissing(t *testing.T) {
func TestConfig_LoadMissing(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

os.RemoveAll("testdir")
_, err := LoadConfigFromDisk("testdir")
tempDir := t.TempDir()
os.RemoveAll(tempDir)
_, err := LoadConfigFromDisk(tempDir)
require.True(t, os.IsNotExist(err))
}

func TestMergeConfig(t *testing.T) {
func TestLocal_MergeConfig(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

os.RemoveAll("testdir")
err := os.Mkdir("testdir", 0777)
tempDir := t.TempDir()

c1 := struct {
GossipFanout int
Expand All @@ -98,7 +96,7 @@ func TestMergeConfig(t *testing.T) {
c1.NetAddress = testString

// write our reduced version of the Local struct
fileToMerge := filepath.Join("testdir", ConfigFilename)
fileToMerge := filepath.Join(tempDir, ConfigFilename)
f, err := os.OpenFile(fileToMerge, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err == nil {
enc := json.NewEncoder(f)
Expand All @@ -110,7 +108,7 @@ func TestMergeConfig(t *testing.T) {

// Take defaultConfig and merge with the saved custom settings.
// This should result in c2 being the same as defaultConfig except for the value(s) in our custom c1
c2, err := mergeConfigFromDir("testdir", defaultConfig)
c2, err := mergeConfigFromDir(tempDir, defaultConfig)

require.NoError(t, err)
require.Equal(t, defaultConfig.Archival || c1.NetAddress != "", c2.Archival)
Expand All @@ -119,12 +117,10 @@ func TestMergeConfig(t *testing.T) {

require.Equal(t, c1.NetAddress, c2.NetAddress)
require.Equal(t, c1.GossipFanout, c2.GossipFanout)

os.RemoveAll("testdir")
}

func saveFullPhonebook(phonebook phonebookBlackWhiteList) error {
filename := filepath.Join("testdir", PhonebookFilename)
func saveFullPhonebook(phonebook phonebookBlackWhiteList, saveToDir string) error {
filename := filepath.Join(saveToDir, PhonebookFilename)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err == nil {
defer f.Close()
Expand All @@ -142,52 +138,48 @@ var phonebookToMerge = phonebookBlackWhiteList{
Include: []string{"test1", "addThisOne"},
}

var expectedMerged = []string{
"test1", "test2", "addThisOne",
}

func TestLoadPhonebook(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

os.RemoveAll("testdir")
err := os.Mkdir("testdir", 0777)
require.NoError(t, err)
tempDir := t.TempDir()

err = saveFullPhonebook(phonebook)
err := saveFullPhonebook(phonebook, tempDir)
require.NoError(t, err)

phonebookEntries, err := LoadPhonebook("testdir")
phonebookEntries, err := LoadPhonebook(tempDir)
require.NoError(t, err)
require.Equal(t, 3, len(phonebookEntries))
for index, entry := range phonebookEntries {
require.Equal(t, phonebook.Include[index], entry)
}
os.RemoveAll("testdir")
}

func TestLoadPhonebookMissing(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

os.RemoveAll("testdir")
_, err := LoadPhonebook("testdir")
tempDir := t.TempDir()
_, err := LoadPhonebook(tempDir)
require.True(t, os.IsNotExist(err))
}

func TestArchivalIfRelay(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

testArchivalIfRelay(t, true)
}

func TestArchivalIfNotRelay(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

testArchivalIfRelay(t, false)
}

func testArchivalIfRelay(t *testing.T, relay bool) {
os.RemoveAll("testdir")
err := os.Mkdir("testdir", 0777)
tempDir := t.TempDir()

c1 := struct {
NetAddress string
Expand All @@ -197,7 +189,7 @@ func testArchivalIfRelay(t *testing.T, relay bool) {
}

// write our reduced version of the Local struct
fileToMerge := filepath.Join("testdir", ConfigFilename)
fileToMerge := filepath.Join(tempDir, ConfigFilename)
f, err := os.OpenFile(fileToMerge, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err == nil {
enc := json.NewEncoder(f)
Expand All @@ -207,18 +199,16 @@ func testArchivalIfRelay(t *testing.T, relay bool) {
require.NoError(t, err)
require.False(t, defaultConfig.Archival, "Default should be non-archival")

c2, err := mergeConfigFromDir("testdir", defaultConfig)
c2, err := mergeConfigFromDir(tempDir, defaultConfig)
require.NoError(t, err)
if relay {
require.True(t, c2.Archival, "Relay should be archival")
} else {
require.False(t, c2.Archival, "Non-relay should still be non-archival")
}

os.RemoveAll("testdir")
}

func TestConfigExampleIsCorrect(t *testing.T) {
func TestLocal_ConfigExampleIsCorrect(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)
Expand Down Expand Up @@ -261,7 +251,7 @@ func loadWithoutDefaults(cfg Local) (Local, error) {
return cfg, err
}

func TestConfigMigrate(t *testing.T) {
func TestLocal_ConfigMigrate(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)
Expand All @@ -288,7 +278,7 @@ func TestConfigMigrate(t *testing.T) {
a.NotEqual(defaultLocal, c0Modified)
}

func TestConfigMigrateFromDisk(t *testing.T) {
func TestLocal_ConfigMigrateFromDisk(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)
Expand All @@ -311,7 +301,7 @@ func TestConfigMigrateFromDisk(t *testing.T) {
}

// Verify that nobody is changing the shipping default configurations
func TestConfigInvariant(t *testing.T) {
func TestLocal_ConfigInvariant(t *testing.T) {
partitiontest.PartitionTest(t)

a := require.New(t)
Expand All @@ -328,8 +318,9 @@ func TestConfigInvariant(t *testing.T) {
}
}

func TestConfigLatestVersion(t *testing.T) {
func TestLocal_ConfigLatestVersion(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

a := require.New(t)

Expand Down Expand Up @@ -391,6 +382,7 @@ func TestConsensusLatestVersion(t *testing.T) {

func TestLocal_DNSBootstrapArray(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type fields struct {
DNSBootstrapID string
Expand Down Expand Up @@ -434,6 +426,7 @@ func TestLocal_DNSBootstrapArray(t *testing.T) {

func TestLocal_DNSBootstrap(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

type fields struct {
DNSBootstrapID string
Expand Down Expand Up @@ -480,8 +473,9 @@ func TestLocal_DNSBootstrap(t *testing.T) {
}
}

func TestLocalStructTags(t *testing.T) {
func TestLocal_StructTags(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

localType := reflect.TypeOf(Local{})

Expand Down Expand Up @@ -523,8 +517,9 @@ func TestLocalStructTags(t *testing.T) {
}
}

func TestGetVersionedDefaultLocalConfig(t *testing.T) {
func TestLocal_GetVersionedDefaultLocalConfig(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

for i := uint32(0); i < getLatestConfigVersion(); i++ {
localVersion := getVersionedDefaultLocalConfig(i)
Expand All @@ -533,8 +528,9 @@ func TestGetVersionedDefaultLocalConfig(t *testing.T) {
}

// TestLocalVersionField - ensures the Version contains only versions tags, the versions are all contiguous, and that no non-version tags are included there.
func TestLocalVersionField(t *testing.T) {
func TestLocal_VersionField(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

localType := reflect.TypeOf(Local{})
field, ok := localType.FieldByName("Version")
Expand All @@ -553,8 +549,9 @@ func TestLocalVersionField(t *testing.T) {
require.Equal(t, expectedTag, string(field.Tag))
}

func TestGetNonDefaultConfigValues(t *testing.T) {
func TestLocal_GetNonDefaultConfigValues(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

cfg := GetDefaultLocal()

Expand Down Expand Up @@ -583,6 +580,8 @@ func TestGetNonDefaultConfigValues(t *testing.T) {

func TestLocal_TxFiltering(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

cfg := GetDefaultLocal()

// ensure the default
Expand All @@ -605,3 +604,62 @@ func TestLocal_TxFiltering(t *testing.T) {
require.True(t, cfg.TxFilterRawMsgEnabled())
require.True(t, cfg.TxFilterCanonicalEnabled())
}

func TestLocal_IsGossipServer(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

cfg := GetDefaultLocal()
require.False(t, cfg.IsGossipServer())

cfg.NetAddress = ":4160"
require.True(t, cfg.IsGossipServer())
}

func TestLocal_RecalculateConnectionLimits(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

var tests = []struct {
maxFDs uint64
reservedIn uint64
restSoftIn uint64
restHardIn uint64
incomingIn int

updated bool
restSoftExp uint64
restHardExp uint64
incomingExp int
}{
{100, 10, 20, 40, 50, false, 20, 40, 50}, // no change
{100, 10, 20, 50, 50, true, 20, 40, 50}, // borrow from rest
{100, 10, 25, 50, 50, true, 25, 40, 50}, // borrow from rest
{100, 10, 50, 50, 50, true, 40, 40, 50}, // borrow from rest, update soft
{100, 10, 9, 19, 81, true, 9, 10, 80}, // borrow from both rest and incoming
{100, 10, 10, 20, 80, true, 10, 10, 80}, // borrow from both rest and incoming
{100, 50, 10, 30, 40, true, 10, 10, 40}, // borrow from both rest and incoming
{100, 90, 10, 30, 40, true, 10, 10, 0}, // borrow from both rest and incoming, clear incoming
{4096, 256, 1024, 2048, 2400, true, 1024, 1440, 2400}, // real numbers
{5000, 256, 1024, 2048, 2400, false, 1024, 2048, 2400}, // real numbers
}

for i, test := range tests {
test := test
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()

c := Local{
RestConnectionsSoftLimit: test.restSoftIn,
RestConnectionsHardLimit: test.restHardIn,
IncomingConnectionsLimit: test.incomingIn,
}
requireFDs := test.reservedIn + test.restHardIn + uint64(test.incomingIn)
res := c.AdjustConnectionLimits(requireFDs, test.maxFDs)
require.Equal(t, test.updated, res)
require.Equal(t, test.restSoftExp, c.RestConnectionsSoftLimit)
require.Equal(t, test.restHardExp, c.RestConnectionsHardLimit)
require.Equal(t, test.incomingExp, c.IncomingConnectionsLimit)
})
}
}
Loading

0 comments on commit 90e744c

Please sign in to comment.