How to use TestBase method of td Package

Best Go-testdeep code snippet using td.TestBase

gorums_test.go

Source:gorums_test.go Github

copy

Full Screen

1package gorums2import (3 "context"4 "crypto/ecdsa"5 "crypto/tls"6 "crypto/x509"7 "net"8 "sync"9 "testing"10 "time"11 "github.com/golang/mock/gomock"12 "github.com/relab/gorums"13 "github.com/relab/hotstuff"14 "github.com/relab/hotstuff/config"15 "github.com/relab/hotstuff/consensus"16 "github.com/relab/hotstuff/crypto/keygen"17 "github.com/relab/hotstuff/eventloop"18 "github.com/relab/hotstuff/internal/testutil"19 "google.golang.org/grpc"20 "google.golang.org/grpc/credentials"21)22func TestConnect(t *testing.T) {23 run := func(t *testing.T, setup setupFunc) {24 const n = 425 ctrl := gomock.NewController(t)26 td := setup(t, ctrl, n)27 builder := testutil.TestModules(t, ctrl, 1, td.keys[0])28 teardown := createServers(t, td, ctrl)29 defer teardown()30 td.builders.Build()31 cfg := NewConfig(td.cfg.ID, td.cfg.Creds, gorums.WithDialTimeout(time.Second))32 builder.Register(cfg)33 builder.Build()34 err := cfg.Connect(&td.cfg)35 if err != nil {36 t.Error(err)37 }38 }39 runBoth(t, run)40}41// testBase is a generic test for a unicast/multicast call42func testBase(t *testing.T, typ interface{}, send func(consensus.Configuration), handle eventloop.EventHandler) {43 run := func(t *testing.T, setup setupFunc) {44 const n = 445 ctrl := gomock.NewController(t)46 td := setup(t, ctrl, n)47 cfg, teardown := createConfig(t, td, ctrl)48 defer teardown()49 td.builders[0].Register(cfg)50 hl := td.builders.Build()51 ctx, cancel := context.WithCancel(context.Background())52 for _, hs := range hl[1:] {53 hs.EventLoop().RegisterHandler(typ, handle)54 go hs.Run(ctx)55 }56 send(cfg)57 cancel()58 }59 runBoth(t, run)60}61func TestPropose(t *testing.T) {62 var wg sync.WaitGroup63 want := consensus.ProposeMsg{64 ID: 1,65 Block: consensus.NewBlock(66 consensus.GetGenesis().Hash(),67 consensus.NewQuorumCert(nil, 0, consensus.GetGenesis().Hash()),68 "foo", 1, 1,69 ),70 }71 testBase(t, want, func(cfg consensus.Configuration) {72 wg.Add(3)73 cfg.Propose(want)74 wg.Wait()75 }, func(event interface{}) {76 got := event.(consensus.ProposeMsg)77 if got.ID != want.ID {78 t.Errorf("wrong id in proposal: got: %d, want: %d", got.ID, want.ID)79 }80 if got.Block.Hash() != want.Block.Hash() {81 t.Error("block hashes do not match")82 }83 wg.Done()84 })85}86func TestTimeout(t *testing.T) {87 var wg sync.WaitGroup88 want := consensus.TimeoutMsg{89 ID: 1,90 View: 1,91 ViewSignature: nil,92 SyncInfo: consensus.NewSyncInfo(),93 }94 testBase(t, want, func(cfg consensus.Configuration) {95 wg.Add(3)96 cfg.Timeout(want)97 wg.Wait()98 }, func(event interface{}) {99 got := event.(consensus.TimeoutMsg)100 if got.ID != want.ID {101 t.Errorf("wrong id in proposal: got: %d, want: %d", got.ID, want.ID)102 }103 if got.View != want.View {104 t.Errorf("wrong view in proposal: got: %d, want: %d", got.View, want.View)105 }106 wg.Done()107 })108}109type testData struct {110 n int111 cfg config.ReplicaConfig112 listeners []net.Listener113 keys []consensus.PrivateKey114 builders testutil.BuilderList115}116type setupFunc func(t *testing.T, ctrl *gomock.Controller, n int) testData117func setupReplicas(t *testing.T, ctrl *gomock.Controller, n int) testData {118 t.Helper()119 listeners := make([]net.Listener, n)120 keys := make([]consensus.PrivateKey, 0, n)121 replicas := make([]*config.ReplicaInfo, 0, n)122 // generate keys and replicaInfo123 for i := 0; i < n; i++ {124 listeners[i] = testutil.CreateTCPListener(t)125 keys = append(keys, testutil.GenerateECDSAKey(t))126 replicas = append(replicas, &config.ReplicaInfo{127 ID: hotstuff.ID(i) + 1,128 Address: listeners[i].Addr().String(),129 PubKey: keys[i].Public(),130 })131 }132 cfg := config.NewConfig(1, keys[0], nil)133 for _, replica := range replicas {134 cfg.Replicas[replica.ID] = replica135 }136 return testData{n, *cfg, listeners, keys, testutil.CreateBuilders(t, ctrl, n, keys...)}137}138func setupTLS(t *testing.T, ctrl *gomock.Controller, n int) testData {139 t.Helper()140 td := setupReplicas(t, ctrl, n)141 certificates := make([]*x509.Certificate, 0, n)142 caPK := testutil.GenerateECDSAKey(t)143 ca, err := keygen.GenerateRootCert(caPK.(*ecdsa.PrivateKey))144 if err != nil {145 t.Fatalf("Failed to generate CA: %v", err)146 }147 for i := 0; i < n; i++ {148 cert, err := keygen.GenerateTLSCert(149 hotstuff.ID(i)+1,150 []string{"localhost", "127.0.0.1"},151 ca,152 td.cfg.Replicas[hotstuff.ID(i)+1].PubKey.(*ecdsa.PublicKey),153 caPK.(*ecdsa.PrivateKey),154 )155 if err != nil {156 t.Fatalf("Failed to generate certificate: %v", err)157 }158 certificates = append(certificates, cert)159 }160 cp := x509.NewCertPool()161 cp.AddCert(ca)162 creds := credentials.NewTLS(&tls.Config{163 RootCAs: cp,164 ClientCAs: cp,165 Certificates: []tls.Certificate{{Certificate: [][]byte{certificates[0].Raw}, PrivateKey: td.keys[0]}},166 ClientAuth: tls.RequireAndVerifyClientCert,167 })168 td.cfg.Creds = creds169 return td170}171func runBoth(t *testing.T, run func(*testing.T, setupFunc)) {172 t.Helper()173 t.Run("NoTLS", func(t *testing.T) { run(t, setupReplicas) })174 t.Run("WithTLS", func(t *testing.T) { run(t, setupTLS) })175}176func createServers(t *testing.T, td testData, ctrl *gomock.Controller) (teardown func()) {177 t.Helper()178 servers := make([]*Server, td.n)179 for i := range servers {180 cfg := td.cfg181 cfg.ID = hotstuff.ID(i + 1)182 cfg.PrivateKey = td.keys[i]183 servers[i] = NewServer(gorums.WithGRPCServerOptions(grpc.Creds(cfg.Creds)))184 servers[i].StartOnListener(td.listeners[i])185 td.builders[i].Register(servers[i])186 }187 return func() {188 for _, srv := range servers {189 srv.Stop()190 }191 }192}193func createConfig(t *testing.T, td testData, ctrl *gomock.Controller) (cfg *Config, teardown func()) {194 t.Helper()195 serverTeardown := createServers(t, td, ctrl)196 cfg = NewConfig(td.cfg.ID, td.cfg.Creds, gorums.WithDialTimeout(time.Second))197 err := cfg.Connect(&td.cfg)198 if err != nil {199 t.Fatal(err)200 }201 return cfg, func() {202 cfg.Close()203 serverTeardown()204 }205}...

Full Screen

Full Screen

backend_test.go

Source:backend_test.go Github

copy

Full Screen

1package backend2import (3 "context"4 "crypto/ecdsa"5 "crypto/tls"6 "crypto/x509"7 "net"8 "sync"9 "testing"10 "time"11 "github.com/golang/mock/gomock"12 "github.com/relab/gorums"13 "github.com/relab/hotstuff"14 "github.com/relab/hotstuff/consensus"15 "github.com/relab/hotstuff/crypto/keygen"16 "github.com/relab/hotstuff/eventloop"17 "github.com/relab/hotstuff/internal/testutil"18 "google.golang.org/grpc"19 "google.golang.org/grpc/credentials"20)21func TestConnect(t *testing.T) {22 run := func(t *testing.T, setup setupFunc) {23 const n = 424 ctrl := gomock.NewController(t)25 td := setup(t, ctrl, n)26 builder := testutil.TestModules(t, ctrl, 1, td.keys[0])27 teardown := createServers(t, td, ctrl)28 defer teardown()29 td.builders.Build()30 cfg := NewConfig(td.creds, gorums.WithDialTimeout(time.Second))31 builder.Register(cfg)32 builder.Build()33 err := cfg.Connect(td.replicas)34 if err != nil {35 t.Error(err)36 }37 }38 runBoth(t, run)39}40// testBase is a generic test for a unicast/multicast call41func testBase(t *testing.T, typ interface{}, send func(consensus.Configuration), handle eventloop.EventHandler) {42 run := func(t *testing.T, setup setupFunc) {43 const n = 444 ctrl := gomock.NewController(t)45 td := setup(t, ctrl, n)46 serverTeardown := createServers(t, td, ctrl)47 defer serverTeardown()48 cfg := NewConfig(td.creds, gorums.WithDialTimeout(time.Second))49 td.builders[0].Register(cfg)50 hl := td.builders.Build()51 err := cfg.Connect(td.replicas)52 if err != nil {53 t.Fatal(err)54 }55 defer cfg.Close()56 ctx, cancel := context.WithCancel(context.Background())57 for _, hs := range hl[1:] {58 hs.EventLoop().RegisterHandler(typ, handle)59 go hs.Run(ctx)60 }61 send(cfg)62 cancel()63 }64 runBoth(t, run)65}66func TestPropose(t *testing.T) {67 var wg sync.WaitGroup68 want := consensus.ProposeMsg{69 ID: 1,70 Block: consensus.NewBlock(71 consensus.GetGenesis().Hash(),72 consensus.NewQuorumCert(nil, 0, consensus.GetGenesis().Hash()),73 "foo", 1, 1,74 ),75 }76 testBase(t, want, func(cfg consensus.Configuration) {77 wg.Add(3)78 cfg.Propose(want)79 wg.Wait()80 }, func(event interface{}) {81 got := event.(consensus.ProposeMsg)82 if got.ID != want.ID {83 t.Errorf("wrong id in proposal: got: %d, want: %d", got.ID, want.ID)84 }85 if got.Block.Hash() != want.Block.Hash() {86 t.Error("block hashes do not match")87 }88 wg.Done()89 })90}91func TestTimeout(t *testing.T) {92 var wg sync.WaitGroup93 want := consensus.TimeoutMsg{94 ID: 1,95 View: 1,96 ViewSignature: nil,97 SyncInfo: consensus.NewSyncInfo(),98 }99 testBase(t, want, func(cfg consensus.Configuration) {100 wg.Add(3)101 cfg.Timeout(want)102 wg.Wait()103 }, func(event interface{}) {104 got := event.(consensus.TimeoutMsg)105 if got.ID != want.ID {106 t.Errorf("wrong id in proposal: got: %d, want: %d", got.ID, want.ID)107 }108 if got.View != want.View {109 t.Errorf("wrong view in proposal: got: %d, want: %d", got.View, want.View)110 }111 wg.Done()112 })113}114type testData struct {115 n int116 creds credentials.TransportCredentials117 replicas []ReplicaInfo118 listeners []net.Listener119 keys []consensus.PrivateKey120 builders testutil.BuilderList121}122type setupFunc func(t *testing.T, ctrl *gomock.Controller, n int) testData123func setupReplicas(t *testing.T, ctrl *gomock.Controller, n int) testData {124 t.Helper()125 listeners := make([]net.Listener, n)126 keys := make([]consensus.PrivateKey, 0, n)127 replicas := make([]ReplicaInfo, 0, n)128 // generate keys and replicaInfo129 for i := 0; i < n; i++ {130 listeners[i] = testutil.CreateTCPListener(t)131 keys = append(keys, testutil.GenerateECDSAKey(t))132 replicas = append(replicas, ReplicaInfo{133 ID: hotstuff.ID(i) + 1,134 Address: listeners[i].Addr().String(),135 PubKey: keys[i].Public(),136 })137 }138 return testData{139 n: n,140 creds: nil,141 replicas: replicas,142 listeners: listeners,143 keys: keys,144 builders: testutil.CreateBuilders(t, ctrl, n, keys...),145 }146}147func setupTLS(t *testing.T, ctrl *gomock.Controller, n int) testData {148 t.Helper()149 td := setupReplicas(t, ctrl, n)150 certificates := make([]*x509.Certificate, 0, n)151 caPK := testutil.GenerateECDSAKey(t)152 ca, err := keygen.GenerateRootCert(caPK.(*ecdsa.PrivateKey))153 if err != nil {154 t.Fatalf("Failed to generate CA: %v", err)155 }156 for i := 0; i < n; i++ {157 cert, err := keygen.GenerateTLSCert(158 hotstuff.ID(i)+1,159 []string{"localhost", "127.0.0.1"},160 ca, //a self-signed TLS certificate to act as a CA.161 td.replicas[i].PubKey.(*ecdsa.PublicKey),162 caPK.(*ecdsa.PrivateKey),163 )164 if err != nil {165 t.Fatalf("Failed to generate certificate: %v", err)166 }167 certificates = append(certificates, cert)168 }169 cp := x509.NewCertPool()170 cp.AddCert(ca)171 creds := credentials.NewTLS(&tls.Config{172 RootCAs: cp,173 ClientCAs: cp,174 Certificates: []tls.Certificate{{Certificate: [][]byte{certificates[0].Raw}, PrivateKey: td.keys[0]}},175 ClientAuth: tls.RequireAndVerifyClientCert,176 })177 td.creds = creds178 return td179}180func runBoth(t *testing.T, run func(*testing.T, setupFunc)) {181 t.Helper()182 t.Run("NoTLS", func(t *testing.T) { run(t, setupReplicas) })183 t.Run("WithTLS", func(t *testing.T) { run(t, setupTLS) })184}185func createServers(t *testing.T, td testData, ctrl *gomock.Controller) (teardown func()) {186 t.Helper()187 servers := make([]*Server, td.n) //n代表新建几个server188 for i := range servers {189 servers[i] = NewServer(gorums.WithGRPCServerOptions(grpc.Creds(td.creds)))190 servers[i].StartOnListener(td.listeners[i])191 td.builders[i].Register(servers[i])192 }193 return func() {194 for _, srv := range servers {195 srv.Stop()196 }197 }198}...

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import (2type td struct {3}4func (t *td) TestBase() {5 fmt.Println("TestBase")6}7type tdd struct {8}9func (t *tdd) TestBase() {10 fmt.Println("TestBase from tdd")11}12func main() {13 t.TestBase()14}15import (16type td struct {17}18func (t *td) TestBase() {19 fmt.Println("TestBase")20}21type tdd struct {22}23func (t *tdd) TestBase() {24 fmt.Println("TestBase from tdd")25}26func main() {27 t.TestBase()28}29import (30type td interface {31 TestBase()32}33type tdd struct {34}35func (t *tdd) TestBase() {36 fmt.Println("TestBase from tdd")37}38func main() {39 t = &tdd{}40 t.TestBase()41}42import (43type td interface {44 TestBase()45}46type tdd struct {47}48func (t *tdd) TestBase() {49 fmt.Println("TestBase from tdd")50}51func main() {52 t = &tdd{}53 t.TestBase()54}55import (56type td interface {57 TestBase()58}59type tdd struct {60}61func (t *tdd) Test

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import (2type testdata struct {3}4func (t testdata) TestBase() int {5}6func main() {7 td := testdata{1, 2}8 fmt.Println(td.TestBase())9}10import (11type testdata struct {12}13func (t *testdata) TestBase() int {14}15func main() {16 td := testdata{1, 2}17 fmt.Println(td.TestBase())18}19In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both. (We’ll see why over the next few pages.)20In general, all methods on a given type should have either value or pointer receivers, but not a mixture of both. (We’ll see why over the next few pages.)21import (22type Vertex struct {23}24func (v *Vertex) Abs() float64 {25 return math.Sqrt(v.X*v.X + v.Y*v.Y)26}27func main() {28 v := &Vertex{3, 4}29 fmt.Println(v.Abs())30}31Methods with pointer receivers can modify the value to which the receiver points (as Scale does here). Since methods often need to modify their

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import "fmt"2type TestBase struct {3}4func (tb TestBase) Method1() {5 fmt.Println("Method1")6}7type TestDerived struct {8}9func (td TestDerived) Method2() {10 fmt.Println("Method2")11}12func main() {13 td := TestDerived{}14 td.Method1()15 td.Method2()16}17import "fmt"18type TestBase struct {19}20func (tb TestBase) Method1() {21 fmt.Println("Method1")22}23type TestDerived struct {24}25func (td TestDerived) Method2() {26 fmt.Println("Method2")27}28func main() {29 td := TestDerived{}30 td.Method1()31 td.Method2()32}

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := NewTableData()4 td.AddRow([]string{"a", "b", "c", "d"})5 td.AddRow([]string{"e", "f", "g", "h"})6 td.AddRow([]string{"i", "j", "k", "l"})7 td.AddRow([]string{"m", "n", "o", "p"})8 td.AddRow([]string{"q", "r", "s", "t"})9 td.AddRow([]string{"u", "v", "w", "x"})10 td.AddRow([]string{"y", "z", "0", "1"})11 td.AddRow([]string{"2", "3", "4", "5"})12 td.AddRow([]string{"6", "7", "8", "9"})13 td.AddRow([]string{"10", "11", "12", "13"})14 td.AddRow([]string{"14", "15", "16", "17"})15 td.AddRow([]string{"18", "19", "20", "21"})16 td.AddRow([]string{"22", "23", "24", "25"})17 td.AddRow([]string{"26", "27", "28", "29"})18 td.AddRow([]string{"30", "31", "32", "33"})19 td.AddRow([]string{"34", "35", "36", "37"})20 td.AddRow([]string{"38", "39", "40", "41"})21 td.AddRow([]string{"42", "43", "44", "45"})22 td.AddRow([]string{"46", "47", "48", "49"})23 td.AddRow([]string{"50", "51", "52", "53"})24 td.AddRow([]string{"54", "55", "56", "57"})25 td.AddRow([]string{"58", "59", "60", "61"})26 td.AddRow([]string{"62", "63", "64", "65"})27 td.AddRow([]string{"66", "67", "68", "69"})28 td.AddRow([]string{"70", "71", "72", "73"})29 td.AddRow([]string{"74", "75", "76

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t.TestBase()4 fmt.Println(t.TestBase())5}6import (7func main() {8 t.TestBase()9 fmt.Println(t.TestBase())10}11import (12func main() {13 t.TestBase()14 fmt.Println(t.TestBase())15}16import (17func main() {18 t.TestBase()19 fmt.Println(t.TestBase())20}21import (22func main() {23 t.TestBase()24 fmt.Println(t.TestBase())25}26import (27func main() {28 t.TestBase()29 fmt.Println(t.TestBase())30}31import (32func main() {33 t.TestBase()34 fmt.Println(t.TestBase())35}36import (37func main() {38 t.TestBase()39 fmt.Println(t.TestBase())40}41import (42func main() {43 t.TestBase()44 fmt.Println(t.TestBase())45}

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 td.TestBase()5}6import "fmt"7type TestBase struct {8}9func (tb TestBase) TestBase() {10 fmt.Println("TestBase method")11}12import "fmt"13type TestDerived struct {14}15func (td TestDerived) TestDerived() {16 fmt.Println("TestDerived method")17}18import (19func TestTestDerived(t *testing.T) {20 td := TestDerived{}21 td.TestDerived()22}23import (24func TestTestBase(t *testing.T) {25 tb := TestBase{}26 tb.TestBase()27}28import (29func TestTestBase(t *testing.T) {30 tb := TestBase{}31 tb.TestBase()32}33import (34func TestTestDerived(t *testing.T) {35 td := TestDerived{}36 td.TestDerived()37}

Full Screen

Full Screen

TestBase

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 td = TestDerived{}4 td.TestBase()5}6func (t TestBase) TestBase() {7 fmt.Println("TestBase")8}9func (t TestDerived) TestBase() {10 fmt.Println("TestDerived")11}12func (t *TestBase) TestBase() {13 fmt.Println("TestBase")14}15func (t *TestDerived) TestBase() {16 fmt.Println("TestDerived")17}18func main() {19 td = TestDerived{}20 td.TestBase()21}22func (t *TestBase) TestBase() {23 fmt.Println("TestBase")24}25func (t *TestDerived) TestBase() {26 fmt.Println("TestDerived")27}28func main() {29 td = TestDerived{}30 td.TestBase()31}32func (t *TestBase) TestBase() {33 fmt.Println("TestBase")34}35func (t *TestDerived) TestBase() {36 fmt.Println("TestDerived")37}38func main() {39 td = TestDerived{}40 td.TestBase()41}42func (t TestBase) TestBase() {43 fmt.Println("TestBase")44}45func (t *TestDerived) TestBase() {46 fmt.Println("TestDerived")47}48func main() {49 td = TestDerived{}50 td.TestBase()51}52func (t TestBase) TestBase() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful