control_test.go 941 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ipv6
  5. import (
  6. "sync"
  7. "testing"
  8. )
  9. func TestControlFlags(t *testing.T) {
  10. tf := FlagInterface | FlagPathMTU
  11. opt := rawOpt{cflags: tf | FlagHopLimit}
  12. // This loop runs methods of raw.Opt concurrently for testing
  13. // concurrent access to the rawOpt. The first entry shold be
  14. // opt.set and the last entry should be opt.clear.
  15. tfns := []func(ControlFlags){opt.set, opt.clear, opt.clear}
  16. ch := make(chan bool)
  17. var wg sync.WaitGroup
  18. for i, fn := range tfns {
  19. wg.Add(1)
  20. go func(i int, fn func(ControlFlags)) {
  21. defer wg.Done()
  22. switch i {
  23. case 0:
  24. close(ch)
  25. case len(tfns) - 1:
  26. <-ch
  27. }
  28. opt.Lock()
  29. defer opt.Unlock()
  30. fn(tf)
  31. }(i, fn)
  32. }
  33. wg.Wait()
  34. if opt.isset(tf) {
  35. t.Fatalf("got %#x; expected %#x", opt.cflags, FlagHopLimit)
  36. }
  37. }