25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

67 satır
1.9 KiB

  1. // Copyright 2016 Google LLC
  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 grpc
  15. import (
  16. "context"
  17. "errors"
  18. "net"
  19. "testing"
  20. "time"
  21. "golang.org/x/oauth2"
  22. "google.golang.org/api/option"
  23. "google.golang.org/grpc"
  24. )
  25. // Check that user optioned grpc.WithDialer option overrides the App Engine hook.
  26. func TestGRPCHook(t *testing.T) {
  27. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
  28. expected := false
  29. appengineDialerHook = (func(ctx context.Context) grpc.DialOption {
  30. return grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
  31. t.Error("did not expect a call to notExpected dialer, got one")
  32. cancel()
  33. return nil, errors.New("not expected")
  34. })
  35. })
  36. defer func() {
  37. appengineDialerHook = nil
  38. }()
  39. expectedDialer := grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
  40. expected = true
  41. cancel()
  42. return nil, errors.New("expected")
  43. })
  44. conn, err := Dial(ctx,
  45. option.WithTokenSource(oauth2.StaticTokenSource(nil)), // No creds.
  46. option.WithGRPCDialOption(expectedDialer),
  47. option.WithEndpoint("example.google.com:443"))
  48. if err != nil {
  49. t.Errorf("DialGRPC: error %v, want nil", err)
  50. }
  51. // gRPC doesn't connect before the first call.
  52. grpc.Invoke(ctx, "foo", nil, nil, conn)
  53. conn.Close()
  54. if !expected {
  55. t.Error("expected a call to expected dialer, didn't get one")
  56. }
  57. }