【问题标题】:gRPC: 14 UNAVAILABLE: failed to connect to all addressesgRPC:14 不可用:无法连接到所有地址
【发布时间】:2020-05-06 11:17:50
【问题描述】:

grpc 无法连接,即使我在超时前将我的 jest test() 异步函数设置为 100000 毫秒上限。

// terminal, after running jest --watch
● creates new record

    14 UNAVAILABLE: failed to connect to all addresses

      at Object.<anonymous>.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
      at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1209:28)
      at InterceptingListener.Object.<anonymous>.InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
      at InterceptingListener.Object.<anonymous>.InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
      at callback (node_modules/grpc/src/client_interceptors.js:847:24)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 skipped, 2 total
Snapshots:   0 total
Time:        106.03s, estimated 116s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.
owner@G700:~/PhpstormProjects/shopify/bu
 FAIL  functions/src/classes/__tests__/FirestoreConnection.test.ts (108.991s)
  ✕ creates new record (100029ms)
  ○ skipped 

  ● creates new record

    : Timeout - Async callback was not invoked within the 100000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 100000ms timeout specified by jest.setTimeout.Error:

      51 | 
      52 | 
    > 53 | test("creates new record", async () => {
         | ^
      54 |   const addedDocument = await db
      55 |     .createNew(RecordTypes.globalRule, {
      56 |       storeId : "dummyStoreId"

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (functions/src/classes/__tests__/FirestoreConnection.test.ts:53:1)

  ● creates new record

    14 UNAVAILABLE: failed to connect to all addresses

      at Object.<anonymous>.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
      at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1209:28)
      at InterceptingListener.Object.<anonymous>.InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
      at InterceptingListener.Object.<anonymous>.InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
      at callback (node_modules/grpc/src/client_interceptors.js:847:24)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 skipped, 2 total
Snapshots:   0 total
Time:        111.16s
Ran all test suites related to changed files.


// FirebaseConnection.ts, inside the class

  protected async addDocument(collectionName: string, documentData: object): Promise<firestore.DocumentSnapshot|null> {
    try {
      const newlyAddedDocument = await this.database
        .collection(collectionName)
        .add(documentData);

      return await newlyAddedDocument.get();
    }
    catch (e) {
      console.log(e, `=====error=====`);
      return null;
    }
  }

  // --------------- Public Methods

  public async createNew(type: RecordTypes, documentData: object): Promise<firestore.DocumentSnapshot|null> {
    this.verifySchemaIsCorrect(type, documentData);
    const collectionName = this.getCollectionName(type);

    return await this.addDocument(collectionName, documentData);
  }

// FirebaseConnection.test.ts
import * as firebaseTesting from "@firebase/testing";
import {RecordTypes} from "../../../../shared";

import FirestoreConnection from "../FirestoreConnection";

/* * * * * * * * * * * * * * * * * * * * *
                  Setup
* * * * * * * * * * * * * * * * * * * * */

const createTestDatabase = (credentials): any => {
  return firebaseTesting
    .initializeTestApp({
      projectId: 'testProject',
      auth: credentials
    })
    .firestore();
};

const nullAllApps = firebaseTesting
  .apps().map(app => app.delete());

const db = new FirestoreConnection('testShopDomain', createTestDatabase(null));


/* * * * * * * * * * * * * * * * * * * * *
                  Tests
* * * * * * * * * * * * * * * * * * * * */

test("creates new record", async () => {
  const addedDocument = await db
    .createNew(RecordTypes.globalRule, {
      storeId : "dummyStoreId"
      , globalPercent : 40
    });

  expect(addedDocument).toEqual({
    storeId : "dummyStoreId"
    , globalPercent : 40
    , badProp : 0
  });
}, 100000);

有人能说出为什么会这样吗?查看文档,这似乎是一个较低级别的库:https://grpc.github.io/grpc/node/

更新

在提示gRPC找不到服务器/模拟器后,我跑了firebase serve --only functions,firestore。终端显示了在不同端口上运行两种服务的模拟器。重新运行 jest --watch 现在会产生一个稍微不同的错误:

 2 UNKNOWN:

      at Object.<anonymous>.exports.createStatusError (node_modules/grpc/src/common.js:91:15)
      at Object.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:1209:28)
      at InterceptingListener.Object.<anonymous>.InterceptingListener._callNext (node_modules/grpc/src/client_interceptors.js:568:42)
      at InterceptingListener.Object.<anonymous>.InterceptingListener.onReceiveStatus (node_modules/grpc/src/client_interceptors.js:618:8)
      at callback (node_modules/grpc/src/client_interceptors.js:847:24)

【问题讨论】:

    标签: node.js jestjs grpc


    【解决方案1】:

    该 gRPC 错误意味着在您尝试连接的地址上没有服务器正在运行,或者由于某种原因无法建立与该服务器的连接。如果您尝试连接到本地 Firestore 模拟器,则应验证它是否正在运行,并且您可以在测试之外连接到它。

    【讨论】:

    • 谢谢,我在运行firebase serve --only functions,firestore后更新了一个新错误
    【解决方案2】:

    Firebase 函数隐藏了潜在的错误。分两步解决:

    1. 使用 firestore.setLogFunction() 在管理 SDK 中启用日志记录
    2. projectId testProject 无效。将 projectId 更改为全小写字母的 testproject 可清除此错误。

    【讨论】:

      【解决方案3】:

      取消设置您的代理 unset https_proxy;unset http_proxy

      【讨论】:

      • 请为您的答案添加解释以及一些来源以支持您的答案。请记住,将来每个问题都会被再次搜索。
      猜你喜欢
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2021-07-19
      • 1970-01-01
      • 2021-12-28
      • 2021-05-12
      • 2021-04-08
      相关资源
      最近更新 更多