Skip to content

Commit

Permalink
unit-test fix
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Dec 18, 2024
1 parent 2eb66c5 commit 816132a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 49 deletions.
4 changes: 3 additions & 1 deletion integration/nwo/common/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func (s *BuildServer) Serve() {
Expect(err).NotTo(HaveOccurred())

s.lis = lis
go utils.IgnoreError(s.server.Serve(lis))
go utils.IgnoreErrorFunc(func() error {
return s.server.Serve(lis)
})
}

func (s *BuildServer) Shutdown(deleteOnStop bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ var packageTestDir = filepath.Join(os.TempDir(), "ccmetadata-validator-test")

func TestGoodIndexJSON(t *testing.T) {
testDir := filepath.Join(packageTestDir, "GoodIndexJSON")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

fileName := "META-INF/statedb/couchdb/indexes/myIndex.json"
Expand All @@ -33,7 +32,6 @@ func TestGoodIndexJSON(t *testing.T) {

func TestBadIndexJSON(t *testing.T) {
testDir := filepath.Join(packageTestDir, "BadIndexJSON")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

fileName := "META-INF/statedb/couchdb/indexes/myIndex.json"
Expand All @@ -53,7 +51,6 @@ func TestBadIndexJSON(t *testing.T) {

func TestIndexWrongLocation(t *testing.T) {
testDir := filepath.Join(packageTestDir, "IndexWrongLocation")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

fileName := "META-INF/statedb/couchdb/myIndex.json"
Expand All @@ -72,7 +69,6 @@ func TestIndexWrongLocation(t *testing.T) {

func TestInvalidMetadataType(t *testing.T) {
testDir := filepath.Join(packageTestDir, "InvalidMetadataType")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

fileName := "myIndex.json"
Expand All @@ -89,7 +85,6 @@ func TestInvalidMetadataType(t *testing.T) {

func TestBadMetadataExtension(t *testing.T) {
testDir := filepath.Join(packageTestDir, "BadMetadataExtension")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

fileName := "myIndex.go"
Expand All @@ -102,7 +97,6 @@ func TestBadMetadataExtension(t *testing.T) {

func TestBadFilePaths(t *testing.T) {
testDir := filepath.Join(packageTestDir, "BadMetadataExtension")
assert.NoError(t, cleanupDir(testDir))
defer utils.IgnoreError(cleanupDir(testDir))

// Test bad META-INF
Expand Down Expand Up @@ -299,7 +293,7 @@ func TestIndexValidationInvalidFields(t *testing.T) {
func cleanupDir(dir string) error {
// clean up any previous files
err := os.RemoveAll(dir)
if err != nil {
if err != nil && !os.IsNotExist(err) {
return err
}
return os.Mkdir(dir, os.ModePerm)
Expand Down
11 changes: 10 additions & 1 deletion platform/common/utils/closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ SPDX-License-Identifier: Apache-2.0

package utils

import "fmt"

type Closer interface {
Close() error
}
Expand All @@ -18,4 +20,11 @@ func CloseMute(closer Closer) {
_ = closer.Close()
}

func IgnoreError(_ error) {}
func IgnoreError(err error) {
// ignore the error
fmt.Printf("IgnoreError: %v\n", err)
}

func IgnoreErrorFunc(f func() error) {
_ = f()
}
4 changes: 3 additions & 1 deletion platform/fabric/core/generic/finality/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func (d *FabricFinality) IsFinal(txID string, address string) error {
return err
}
eventCh = make(chan delivery.TxEvent, 1)
go utils.IgnoreError(delivery.DeliverReceive(deliverStream, address, txID, eventCh))
go utils.IgnoreErrorFunc(func() error {
return delivery.DeliverReceive(deliverStream, address, txID, eventCh)
})
committed, _, _, err := delivery.DeliverWaitForResponse(ctx, eventCh, txID)
if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion platform/orion/core/generic/delivery/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func New(network Network, callback Callback, vault Vault, waitForEventTimeout ti

// StartDelivery runs the delivery service in a goroutine
func (d *delivery) StartDelivery(ctx context.Context) error {
go utils.IgnoreError(d.Run(ctx))
go utils.IgnoreErrorFunc(func() error {
return d.Run(ctx)
})
return nil
}

Expand Down
12 changes: 8 additions & 4 deletions platform/view/services/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ func TestNewConnection(t *testing.T) {
}
srv := grpc.NewServer(serverOpts...)
defer srv.Stop()
go utils.IgnoreError(srv.Serve(lis))
go utils.IgnoreErrorFunc(func() error {
return srv.Serve(lis)
})
client, err := grpc3.NewGRPCClient(test.config)
if err != nil {
t.Fatalf("error creating client for test: %v", err)
Expand Down Expand Up @@ -393,7 +395,9 @@ func TestSetServerRootCAs(t *testing.T) {
Certificates: []tls.Certificate{testCerts.serverCert},
})))
defer srv.Stop()
go utils.IgnoreError(srv.Serve(lis))
go utils.IgnoreErrorFunc(func() error {
return srv.Serve(lis)
})

// initial config should work
t.Log("running initial good config")
Expand Down Expand Up @@ -444,7 +448,7 @@ func TestSetMessageSize(t *testing.T) {
}
testpb.RegisterEchoServiceServer(srv.Server(), &echoServer{})
defer srv.Stop()
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)

var tests = []struct {
name string
Expand Down Expand Up @@ -627,7 +631,7 @@ func TestDynamicClientTLSLoading(t *testing.T) {

go func() {
defer wg.Done()
utils.IgnoreError(server.Start())
_ = server.Start()
}()

var dynamicRootCerts atomic.Value
Expand Down
22 changes: 11 additions & 11 deletions platform/view/services/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func TestNewGRPCServer(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed
Expand Down Expand Up @@ -561,7 +561,7 @@ func TestNewGRPCServerFromListener(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed
Expand Down Expand Up @@ -605,7 +605,7 @@ func TestNewSecureGRPCServer(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed
Expand Down Expand Up @@ -689,7 +689,7 @@ func TestVerifyCertificateCallback(t *testing.T) {
VerifyCertificate: verifyFunc,
},
})
go utils.IgnoreError(gRPCServer.Start())
go utils.IgnoreErrorFunc(gRPCServer.Start)
defer gRPCServer.Stop()

t.Run("Success path", func(t *testing.T) {
Expand Down Expand Up @@ -736,7 +736,7 @@ func TestWithSignedRootCertificates(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed
Expand Down Expand Up @@ -801,7 +801,7 @@ func TestWithSignedIntermediateCertificates(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed
Expand Down Expand Up @@ -855,7 +855,7 @@ func runMutualAuth(t *testing.T, servers []testServer, trustedClients, unTrusted

// register the GRPC test server and start the GRPCServer
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed but just in case
Expand Down Expand Up @@ -965,7 +965,7 @@ func TestSetClientRootCAs(t *testing.T) {

// register the GRPC test server and start the GRPCServer
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// should not be needed but just in case
Expand Down Expand Up @@ -1058,7 +1058,7 @@ func TestUpdateTLSCert(t *testing.T) {
assert.NoError(t, err)
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

certPool := x509.NewCertPool()
Expand Down Expand Up @@ -1172,7 +1172,7 @@ func TestCipherSuites(t *testing.T) {
testAddress := lis.Addr().String()
srv, err := grpc3.NewGRPCServerFromListener(lis, serverConfig)
assert.NoError(t, err)
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)

for _, test := range tests {
test := test
Expand Down Expand Up @@ -1229,7 +1229,7 @@ func TestServerInterceptors(t *testing.T) {
assert.NoError(t, err, "failed to create gRPC server")
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})
defer srv.Stop()
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)

_, err = invokeEmptyCall(
lis.Addr().String(),
Expand Down
2 changes: 1 addition & 1 deletion platform/view/services/grpc/serverstatshandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestConnMetricsGRPCServer(t *testing.T) {
testpb.RegisterEmptyServiceServer(srv.Server(), &emptyServiceServer{})

// start the server
go utils.IgnoreError(srv.Start())
go utils.IgnoreErrorFunc(srv.Start)
defer srv.Stop()

// test grpc connection counts
Expand Down
6 changes: 4 additions & 2 deletions platform/view/services/grpc/tlsgen/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ func TestTLSCA(t *testing.T) {
srv := createTLSService(t, ca, "127.0.0.1")
listener, err := net.Listen("tcp", "127.0.0.1:0")
assert.NoError(t, err)
go utils.IgnoreError(srv.Serve(listener))
go utils.IgnoreErrorFunc(func() error {
return srv.Serve(listener)
})
defer srv.Stop()
defer utils.IgnoreError(listener.Close())
defer utils.IgnoreErrorFunc(listener.Close)

probeTLS := func(kp *CertKeyPair) error {
cert, err := tls.X509KeyPair(kp.Cert, kp.Key)
Expand Down
34 changes: 22 additions & 12 deletions platform/view/services/metrics/statsd/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var _ = Describe("Provider", func() {
for _, alpha := range []string{"x", "y", "z"} {
counter.With("alpha", alpha, "beta", "b").Add(1)
buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name.%s.b:%f|c\n", alpha, float64(1))))
}
})
Expand All @@ -68,7 +69,8 @@ var _ = Describe("Provider", func() {
counter.Add(1)

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal("namespace.subsystem.name:1.000000|c\n"))
})
})
Expand All @@ -85,7 +87,8 @@ var _ = Describe("Provider", func() {
counter.Add(float64(i))

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name:%f|c\n", float64(i))))
}
})
Expand Down Expand Up @@ -123,7 +126,8 @@ var _ = Describe("Provider", func() {
for _, alpha := range []string{"x", "y", "z"} {
gauge.With("alpha", alpha, "beta", "b").Set(float64(i))
buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name.%s.b:%f|g\n", alpha, float64(i))))
}
}
Expand All @@ -133,15 +137,16 @@ var _ = Describe("Provider", func() {
gauge := provider.NewGauge(gaugeOpts)

for _, alpha := range []string{"x", "y", "z"} {
gauge.With("alpha", alpha, "beta", "b").Set(float64(1.0))
gauge.With("alpha", alpha, "beta", "b").Set(1.0)
}
for _, alpha := range []string{"x", "y", "z"} {
for i := 0; i < 5; i++ {
gauge.With("alpha", alpha, "beta", "b").Add(float64(1.0))
gauge.With("alpha", alpha, "beta", "b").Add(1.0)
}
}
buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(strings.SplitN(buf.String(), "\n", -1)).To(ConsistOf(
Equal("namespace.subsystem.name.x.b:6.000000|g"),
Equal("namespace.subsystem.name.y.b:6.000000|g"),
Expand All @@ -161,7 +166,8 @@ var _ = Describe("Provider", func() {
gauge.Set(1)

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal("namespace.subsystem.name:1.000000|g\n"))
})
})
Expand All @@ -178,7 +184,8 @@ var _ = Describe("Provider", func() {
gauge.Add(float64(i))

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name:%f|g\n", float64(i))))
}
})
Expand Down Expand Up @@ -226,7 +233,8 @@ var _ = Describe("Provider", func() {
for _, alpha := range []string{"x", "y", "z"} {
histogram.With("alpha", alpha, "beta", "b").Observe(float64(i))
buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name.%s.b:%f|ms\n", alpha, float64(i))))
}
}
Expand All @@ -243,7 +251,8 @@ var _ = Describe("Provider", func() {
histogram.Observe(1)

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal("namespace.subsystem.name:1.000000|ms\n"))
})
})
Expand All @@ -260,7 +269,8 @@ var _ = Describe("Provider", func() {
histogram.Observe(float64(i))

buf := &bytes.Buffer{}
Expect(s.WriteTo(buf)).To(Succeed())
_, err := s.WriteTo(buf)
Expect(err).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf("namespace.subsystem.name:%f|ms\n", float64(i))))
}
})
Expand Down
Loading

0 comments on commit 816132a

Please sign in to comment.