Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
[FAB-11082] bubble errors up to main
Browse files Browse the repository at this point in the history
 - fabproxy construction now panics on programmer error
 - fabproxy start errors are printed

Change-Id: I0bff8dc807e56441291ed20a31fade1e4f99c2bf
Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
  • Loading branch information
MHBauer authored and swetharepakula committed Sep 13, 2018
1 parent 57f6194 commit 44e412f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
6 changes: 5 additions & 1 deletion fabproxy/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func main() {

fmt.Printf("Starting Fab Proxy on port %d\n", portNumber)
proxy := fabproxy.NewFabProxy(ethService)
proxy.Start(portNumber)
err = proxy.Start(portNumber)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer func() {
fmt.Println("Shutting down the Fab Proxy")
proxy.Shutdown()
Expand Down
13 changes: 9 additions & 4 deletions fabproxy/fabproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ func NewFabProxy(service EthService) *FabProxy {
}

rpcServer.RegisterCodec(NewRPCCodec(), "application/json")
rpcServer.RegisterService(service, "eth")
rpcServer.RegisterService(&NetService{}, "net")
msg := "this panic indicates a programming error, and is unreachable"
if err := rpcServer.RegisterService(service, "eth"); err != nil {
panic(msg)
}
if err := rpcServer.RegisterService(&NetService{}, "net"); err != nil {
panic(msg)
}
return proxy
}

func (p *FabProxy) Start(port int) {
func (p *FabProxy) Start(port int) error {
r := mux.NewRouter()
r.Handle("/", p.rpcServer)

Expand All @@ -43,7 +48,7 @@ func (p *FabProxy) Start(port int) {
allowedMethods := handlers.AllowedMethods([]string{"POST"})

p.httpServer = &http.Server{Handler: handlers.CORS(allowedHeaders, allowedOrigins, allowedMethods)(r), Addr: fmt.Sprintf(":%d", port)}
fmt.Println(p.httpServer.ListenAndServe())
return p.httpServer.ListenAndServe()
}

func (p *FabProxy) Shutdown() error {
Expand Down
39 changes: 37 additions & 2 deletions fabproxy/fabproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"

"github.com/hyperledger/fabric-chaincode-evm/fabproxy"
Expand All @@ -31,15 +32,18 @@ var _ = Describe("Fabproxy", func() {
req *http.Request
proxyDoneChan chan struct{}
client *http.Client
port int
)

BeforeEach(func() {
port := config.GinkgoConfig.ParallelNode + 5000
port = config.GinkgoConfig.ParallelNode + 5000
mockEthService = &mocks.MockEthService{}
client = &http.Client{}

proxyDoneChan = make(chan struct{}, 1)
var err error
proxy = fabproxy.NewFabProxy(mockEthService)
Expect(err).ToNot(HaveOccurred())

go func(proxy *fabproxy.FabProxy, proxyDoneChan chan struct{}) {
proxy.Start(port)
Expand All @@ -65,7 +69,6 @@ var _ = Describe("Fabproxy", func() {
}

//curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "0x2"],"id":1}'
var err error
body := strings.NewReader(`{"jsonrpc":"2.0","method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b"],"id":1}`)
req, err = http.NewRequest("POST", proxyAddr, body)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -167,3 +170,35 @@ var _ = Describe("Fabproxy", func() {
})
})
})

var _ = Describe("fabproxy fails to start", func() {
var (
ln net.Listener
err error
)
Context("when the port is already bound", func() {

port := config.GinkgoConfig.ParallelNode + 5000
portstr := strconv.Itoa(port)

BeforeEach(func() {
By("binds the port " + portstr)
ln, err = net.Listen("tcp", ":"+portstr)
Expect(err).ToNot(HaveOccurred())
})

mockEthService := &mocks.MockEthService{}
proxy := fabproxy.NewFabProxy(mockEthService)

It("exits instead of starting", func() {
err := proxy.Start(port)
Expect(err).To(HaveOccurred())
})

AfterEach(func() {
By("releasing the port")
err := ln.Close()
Expect(err).ToNot(HaveOccurred())
})
})
})

0 comments on commit 44e412f

Please sign in to comment.