interaction_env_handler_deliver_msgs.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafttest
  15. import (
  16. "fmt"
  17. "strconv"
  18. "testing"
  19. "github.com/cockroachdb/datadriven"
  20. "go.etcd.io/etcd/raft"
  21. "go.etcd.io/etcd/raft/raftpb"
  22. )
  23. func (env *InteractionEnv) handleDeliverMsgs(t *testing.T, d datadriven.TestData) error {
  24. var rs []Recipient
  25. for _, arg := range d.CmdArgs {
  26. if len(arg.Vals) == 0 {
  27. id, err := strconv.ParseUint(arg.Key, 10, 64)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. rs = append(rs, Recipient{ID: id})
  32. }
  33. for i := range arg.Vals {
  34. switch arg.Key {
  35. case "drop":
  36. var id uint64
  37. arg.Scan(t, i, &id)
  38. var found bool
  39. for _, r := range rs {
  40. if r.ID == id {
  41. found = true
  42. }
  43. }
  44. if found {
  45. t.Fatalf("can't both deliver and drop msgs to %d", id)
  46. }
  47. rs = append(rs, Recipient{ID: id, Drop: true})
  48. }
  49. }
  50. }
  51. if n := env.DeliverMsgs(rs...); n == 0 {
  52. env.Output.WriteString("no messages\n")
  53. }
  54. return nil
  55. }
  56. type Recipient struct {
  57. ID uint64
  58. Drop bool
  59. }
  60. // DeliverMsgs goes through env.Messages and, depending on the Drop flag,
  61. // delivers or drops messages to the specified Recipients. Returns the
  62. // number of messages handled (i.e. delivered or dropped). A handled message
  63. // is removed from env.Messages.
  64. func (env *InteractionEnv) DeliverMsgs(rs ...Recipient) int {
  65. var n int
  66. for _, r := range rs {
  67. var msgs []raftpb.Message
  68. msgs, env.Messages = splitMsgs(env.Messages, r.ID)
  69. n += len(msgs)
  70. for _, msg := range msgs {
  71. if r.Drop {
  72. fmt.Fprint(env.Output, "dropped: ")
  73. }
  74. fmt.Fprintln(env.Output, raft.DescribeMessage(msg, defaultEntryFormatter))
  75. if r.Drop {
  76. // NB: it's allowed to drop messages to nodes that haven't been instantiated yet,
  77. // we haven't used msg.To yet.
  78. continue
  79. }
  80. toIdx := int(msg.To - 1)
  81. if err := env.Nodes[toIdx].Step(msg); err != nil {
  82. env.Output.WriteString(err.Error())
  83. }
  84. }
  85. }
  86. return n
  87. }