github.com/iDigitalFlame/xmt@v0.5.4/c2/cfg/setting_test.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package cfg
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestConfig(t *testing.T) {
    26  	c := Pack(
    27  		Host("127.0.0.1:8085"),
    28  		ConnectTCP,
    29  		Sleep(time.Second*5),
    30  		Jitter(0),
    31  		Weight(10),
    32  		KillDate(time.Now().AddDate(1, 0, 0)),
    33  	)
    34  	c.AddGroup(
    35  		Host("127.0.0.1:8086"),
    36  		ConnectTCP,
    37  		Sleep(time.Second*10),
    38  		Jitter(50),
    39  		&WorkHours{Days: DayFriday},
    40  	)
    41  	c.AddGroup(
    42  		Host("127.0.0.1:8087"),
    43  		ConnectTCP,
    44  		Sleep(time.Second*5),
    45  		Jitter(0),
    46  	)
    47  	c.AddGroup(Host("127.0.0.1:8088")) // Invalid
    48  	c.Add(SelectorLastValid)
    49  	p, err := Raw(c.Bytes())
    50  	if err != nil {
    51  		t.Fatalf("TestConfig(): Raw failed with error: %s!", err.Error())
    52  	}
    53  	v, ok := p.(*Group)
    54  	if !ok {
    55  		t.Fail()
    56  	}
    57  	if v.Jitter() != 0 {
    58  		t.Fatalf(`TestConfig(): First Jitter should be "0", but is "%d"!`, v.Jitter())
    59  	}
    60  	if h, _, _ := v.Next(); h != "127.0.0.1:8085" {
    61  		t.Fatalf(`TestConfig(): First Host should be "127.0.0.1:8085", but is "%s"!`, h)
    62  	}
    63  	v.Switch(false)
    64  	if v.Jitter() != 0 {
    65  		t.Fatalf(`TestConfig(): First Jitter should be "0", but is "%d"!`, v.Jitter())
    66  	}
    67  	if h, _, _ := v.Next(); h != "127.0.0.1:8085" {
    68  		t.Fatalf(`TestConfig(): First Host should be "127.0.0.1:8085", but is "%s"!`, h)
    69  	}
    70  	v.Switch(true)
    71  	if v.Jitter() != 50 {
    72  		t.Fatalf(`TestConfig(): Second Jitter should be "50", but is "%d"!`, v.Jitter())
    73  	}
    74  	if h, _, _ := v.Next(); h != "127.0.0.1:8086" {
    75  		t.Fatalf(`TestConfig(): Second Host should be "127.0.0.1:8086", but is "%s"!`, h)
    76  	}
    77  	v.Switch(true) // Advance 2 places
    78  	v.Switch(true)
    79  	if _, err := v.Connect(context.Background(), ""); err != ErrNotAConnector {
    80  		t.Fatalf(`TestConfig(): Last Group should raise "ErrNotAConnector" but instead got: %s!`, err)
    81  	}
    82  }