Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/code.cloudfoundry.org/gorouter/mbus/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type RegistryMessage struct {
type RegistryMessageOpts struct {
LoadBalancingAlgorithm string `json:"loadbalancing"`
HashHeaderName string `json:"hash_header"`
HashBalance float64 `json:"hash_balance"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please implement a new unit test in subscriber_test.go to ensure this regression is avoided during NATS message registration.

Something like:

It("parses hash_balance from the incoming JSON string", func() {
				data := []byte(`{"host":"host","app":"app","protocol":"http2","uris":["test.example.com"],"options":{"loadbalancing":"hash","hash_header":"X-Header","hash_balance":"1.5"}}`)

				err := natsClient.Publish("router.register", data)
				Expect(err).ToNot(HaveOccurred())

				Eventually(registry.RegisterCallCount).Should(Equal(1))
				_, originalEndpoint := registry.RegisterArgsForCall(0)
				expectedEndpoint := route.NewEndpoint(&route.EndpointOpts{
					Host:                   "host",
					AppId:                  "app",
					Protocol:               "http2",
					LoadBalancingAlgorithm: "hash",
					HashHeaderName:         "X-Header",
					HashBalanceFactor:      1.5,
				})

				Expect(originalEndpoint).To(Equal(expectedEndpoint))
			})

HashBalance float64 `json:"hash_balance,string"`
// RFC route policy options (from Cloud Controller via Diego sync)
RoutePolicyScope string `json:"route_policy_scope,omitempty"`
RoutePolicySources string `json:"route_policy_sources,omitempty"`
Expand Down
29 changes: 29 additions & 0 deletions src/code.cloudfoundry.org/gorouter/mbus/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,35 @@ var _ = Describe("Subscriber", func() {
Expect(expectedEndpoint.HashBalanceFactor).To(Equal(0.0))
Expect(originalEndpoint).To(Equal(expectedEndpoint))
})

It("parses hash_balance from incoming JSON as a string", func() {
data := []byte(`{"host":"host","app":"app","uris":["test.example.com"],"options":{"loadbalancing":"hash","hash_header":"X-Header","hash_balance":"1.5"}}`)

err := natsClient.Publish("router.register", data)
Expect(err).ToNot(HaveOccurred())

Eventually(registry.RegisterCallCount).Should(Equal(1))
_, originalEndpoint := registry.RegisterArgsForCall(0)
expectedEndpoint := route.NewEndpoint(&route.EndpointOpts{
Host: "host",
AppId: "app",
Protocol: "http2",
LoadBalancingAlgorithm: "hash",
HashHeaderName: "X-Header",
HashBalanceFactor: 1.5,
})

Expect(originalEndpoint).To(Equal(expectedEndpoint))
})

It("rejects route if hash_balance value is not a string", func() {
data := []byte(`{"host":"host","app":"app","uris":["test.example.com"],"options":{"loadbalancing":"hash","hash_header":"X-Header","hash_balance":1.5}}`)

err := natsClient.Publish("router.register", data)
Expect(err).ToNot(HaveOccurred())

Consistently(registry.RegisterCallCount).Should(BeZero())
})
})
})

Expand Down