@@ -239,6 +239,7 @@ func TestNearest(t *testing.T) {
239239 name string
240240 service string
241241 typ string
242+ country string
242243 lat float64
243244 lon float64
244245 instances []v2.HeartbeatMessage
@@ -250,6 +251,7 @@ func TestNearest(t *testing.T) {
250251 name : "NDT7-any-type" ,
251252 service : "ndt/ndt7" ,
252253 typ : "" ,
254+ country : "US" ,
253255 lat : 43.1988 ,
254256 lon : - 75.3242 ,
255257 expectedTargets : []v2.Target {virtualTarget , physicalTarget },
@@ -260,6 +262,7 @@ func TestNearest(t *testing.T) {
260262 name : "NDT7-physical" ,
261263 service : "ndt/ndt7" ,
262264 typ : "physical" ,
265+ country : "US" ,
263266 lat : 43.1988 ,
264267 lon : - 75.3242 ,
265268 expectedTargets : []v2.Target {physicalTarget },
@@ -270,6 +273,7 @@ func TestNearest(t *testing.T) {
270273 name : "NDT7-virtual" ,
271274 service : "ndt/ndt7" ,
272275 typ : "virtual" ,
276+ country : "US" ,
273277 lat : 43.1988 ,
274278 lon : - 75.3242 ,
275279 expectedTargets : []v2.Target {virtualTarget },
@@ -280,6 +284,7 @@ func TestNearest(t *testing.T) {
280284 name : "wehe" ,
281285 service : "wehe/replay" ,
282286 typ : "" ,
287+ country : "US" ,
283288 lat : 43.1988 ,
284289 lon : - 75.3242 ,
285290 expectedTargets : []v2.Target {weheTarget },
@@ -305,7 +310,7 @@ func TestNearest(t *testing.T) {
305310 locator .UpdateHealth (i .Registration .Hostname , * i .Health )
306311 }
307312
308- gotTargets , gotURLs , err := locator .Nearest (tt .service , tt .typ , tt .lat , tt .lon )
313+ gotTargets , gotURLs , err := locator .Nearest (tt .service , tt .typ , "" , tt .lat , tt .lon )
309314
310315 if ! reflect .DeepEqual (gotTargets , tt .expectedTargets ) {
311316 t .Errorf ("Nearest() targets got: %+v, want %+v" , gotTargets , tt .expectedTargets )
@@ -335,6 +340,7 @@ func TestFilterSites(t *testing.T) {
335340 name string
336341 service string
337342 typ string
343+ country string
338344 lat float64
339345 lon float64
340346 expected []site
@@ -343,6 +349,7 @@ func TestFilterSites(t *testing.T) {
343349 name : "NDT7-any-type" ,
344350 service : "ndt/ndt7" ,
345351 typ : "" ,
352+ country : "US" ,
346353 lat : 43.1988 ,
347354 lon : - 75.3242 ,
348355 expected : []site {virtualSite , physicalSite },
@@ -351,6 +358,7 @@ func TestFilterSites(t *testing.T) {
351358 name : "NDT7-physical" ,
352359 service : "ndt/ndt7" ,
353360 typ : "physical" ,
361+ country : "US" ,
354362 lat : 43.1988 ,
355363 lon : - 75.3242 ,
356364 expected : []site {physicalSite },
@@ -359,6 +367,7 @@ func TestFilterSites(t *testing.T) {
359367 name : "NDT7-virtual" ,
360368 service : "ndt/ndt7" ,
361369 typ : "virtual" ,
370+ country : "US" ,
362371 lat : 43.1988 ,
363372 lon : - 75.3242 ,
364373 expected : []site {virtualSite },
@@ -367,6 +376,7 @@ func TestFilterSites(t *testing.T) {
367376 name : "wehe" ,
368377 service : "wehe/replay" ,
369378 typ : "" ,
379+ country : "US" ,
370380 lat : 43.1988 ,
371381 lon : - 75.3242 ,
372382 expected : []site {weheSite },
@@ -375,6 +385,7 @@ func TestFilterSites(t *testing.T) {
375385 name : "too-far" ,
376386 service : "ndt-ndt7" ,
377387 typ : "" ,
388+ country : "" ,
378389 lat : 1000 ,
379390 lon : 1000 ,
380391 expected : []site {},
@@ -383,7 +394,7 @@ func TestFilterSites(t *testing.T) {
383394
384395 for _ , tt := range tests {
385396 t .Run (tt .name , func (t * testing.T ) {
386- got := filterSites (tt .service , tt .typ , tt .lat , tt .lon , instances )
397+ got := filterSites (tt .service , tt .typ , tt .country , tt . lat , tt .lon , instances )
387398
388399 sortSites (got )
389400 for _ , v := range got {
@@ -766,3 +777,60 @@ func TestPickWithProbability(t *testing.T) {
766777 })
767778 }
768779}
780+
781+ func TestBiasedDistance (t * testing.T ) {
782+ tests := []struct {
783+ name string
784+ country string
785+ r * v2.Registration
786+ distance float64
787+ want float64
788+ }{
789+ {
790+ name : "empty-country" ,
791+ country : "" ,
792+ r : & v2.Registration {
793+ CountryCode : "foo" ,
794+ },
795+ distance : 100 ,
796+ want : 100 ,
797+ },
798+ {
799+ name : "unknown-country" ,
800+ country : "ZZ" ,
801+ r : & v2.Registration {
802+ CountryCode : "foo" ,
803+ },
804+ distance : 100 ,
805+ want : 100 ,
806+ },
807+ {
808+ name : "same-country" ,
809+ country : "foo" ,
810+ r : & v2.Registration {
811+ CountryCode : "foo" ,
812+ },
813+ distance : 100 ,
814+ want : 100 ,
815+ },
816+ {
817+ name : "different-country" ,
818+ country : "bar" ,
819+ r : & v2.Registration {
820+ CountryCode : "foo" ,
821+ },
822+ distance : 100 ,
823+ want : 200 ,
824+ },
825+ }
826+
827+ for _ , tt := range tests {
828+ t .Run (tt .name , func (t * testing.T ) {
829+ got := biasedDistance (tt .country , tt .r , tt .distance )
830+
831+ if got != tt .want {
832+ t .Errorf ("biasedDistance() got: %f, want: %f" , got , tt .want )
833+ }
834+ })
835+ }
836+ }
0 commit comments