(t *testing.T)
| 431 | } |
| 432 | |
| 433 | func TestSwitchStopPeerForError(t *testing.T) { |
| 434 | s := httptest.NewServer(promhttp.Handler()) |
| 435 | defer s.Close() |
| 436 | |
| 437 | scrapeMetrics := func() string { |
| 438 | resp, err := http.Get(s.URL) |
| 439 | require.NoError(t, err) |
| 440 | defer resp.Body.Close() |
| 441 | buf, _ := io.ReadAll(resp.Body) |
| 442 | return string(buf) |
| 443 | } |
| 444 | |
| 445 | namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers" |
| 446 | re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`) |
| 447 | peersMetricValue := func() float64 { |
| 448 | matches := re.FindStringSubmatch(scrapeMetrics()) |
| 449 | f, _ := strconv.ParseFloat(matches[1], 64) |
| 450 | return f |
| 451 | } |
| 452 | |
| 453 | p2pMetrics := PrometheusMetrics(namespace) |
| 454 | |
| 455 | // make two connected switches |
| 456 | sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch { |
| 457 | // set metrics on sw1 |
| 458 | if i == 0 { |
| 459 | opt := WithMetrics(p2pMetrics) |
| 460 | opt(sw) |
| 461 | } |
| 462 | return initSwitchFunc(i, sw) |
| 463 | }) |
| 464 | |
| 465 | assert.Equal(t, len(sw1.Peers().List()), 1) |
| 466 | assert.EqualValues(t, 1, peersMetricValue()) |
| 467 | |
| 468 | // send messages to the peer from sw1 |
| 469 | p := sw1.Peers().List()[0] |
| 470 | SendEnvelopeShim(p, Envelope{ |
| 471 | ChannelID: 0x1, |
| 472 | Message: &p2pproto.Message{}, |
| 473 | }, sw1.Logger) |
| 474 | |
| 475 | // stop sw2. this should cause the p to fail, |
| 476 | // which results in calling StopPeerForError internally |
| 477 | t.Cleanup(func() { |
| 478 | if err := sw2.Stop(); err != nil { |
| 479 | t.Error(err) |
| 480 | } |
| 481 | }) |
| 482 | |
| 483 | // now call StopPeerForError explicitly, eg. from a reactor |
| 484 | sw1.StopPeerForError(p, fmt.Errorf("some err")) |
| 485 | |
| 486 | assert.Equal(t, len(sw1.Peers().List()), 0) |
| 487 | assert.EqualValues(t, 0, peersMetricValue()) |
| 488 | } |
| 489 | |
| 490 | func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) { |
nothing calls this directly
no test coverage detected