interaction_env_handler_deliver_msgs.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "errors"
  17. "fmt"
  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. if len(env.Messages) == 0 {
  25. return errors.New("no messages to deliver")
  26. }
  27. msgs := env.Messages
  28. env.Messages = nil
  29. return env.DeliverMsgs(msgs)
  30. }
  31. // DeliverMsgs delivers the supplied messages typically taken from env.Messages.
  32. func (env *InteractionEnv) DeliverMsgs(msgs []raftpb.Message) error {
  33. for _, msg := range msgs {
  34. toIdx := int(msg.To - 1)
  35. var drop bool
  36. if toIdx >= len(env.Nodes) {
  37. // Drop messages for peers that don't exist yet.
  38. drop = true
  39. env.Output.WriteString("dropped: ")
  40. }
  41. fmt.Fprintln(env.Output, raft.DescribeMessage(msg, defaultEntryFormatter))
  42. if drop {
  43. continue
  44. }
  45. if err := env.Nodes[toIdx].Step(msg); err != nil {
  46. env.Output.WriteString(err.Error())
  47. continue
  48. }
  49. }
  50. return nil
  51. }