googleapis / nodejs-language

This repository is deprecated. All of its content and history has been moved to googleapis/google-cloud-node.
https://cloud.google.com/natural-language/
Apache License 2.0
127 stars 99 forks source link

UnhandledPromiseRejectionWarning: The client has already been closed #496

Closed gustawdaniel closed 3 years ago

gustawdaniel commented 4 years ago

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

1) Is this a client library issue or a product issue? This is the client library problem.

2) Did someone already solve this?

No. This is error connected directly with this package.

3) Do you have a support contract?

No. I found

a) lack of documentation b) problem in source code

Environment details

Steps to reproduce

  1. Letc create standard Test.
import {analyse} from "../api/helpers/sentimentAnalyser";

const inputSentence = 'Nienawidzę kradzieży i rozbojów. Na szczęście miłość, jest cudowna.';

const correctOutput = {
    inputSentence,
    translation: 'I hate robbery and theft. Fortunately, love is wonderful.',
    sentences: [
        {
            text: {content: 'I hate robbery and theft.', beginOffset: -1},
            sentiment: {magnitude: 0.8999999761581421, score: -0.8999999761581421}
        },
        {
            text: {content: 'Fortunately, love is wonderful.', beginOffset: -1},
            sentiment: {magnitude: 0.8999999761581421, score: 0.8999999761581421}
        }
    ],
    documentSentiment: {magnitude: 1.7999999523162842, score: 0},
    language: 'en'
};

it('Sentiment should be able to estimate', async () => {
    const obtainedResult = await analyse(inputSentence);
    expect(obtainedResult).toEqual(correctOutput);
});

With function

import {SentimentSummary} from "../interfaces/SentimentSummary";
const language = require('@google-cloud/language');
const translate = require('@vitalets/google-translate-api');

process.env.GOOGLE_APPLICATION_CREDENTIALS = `${process.cwd()}/google-master-key.json`;

export const analyse = async (input: string): Promise<SentimentSummary> => {
    let client = new language.LanguageServiceClient();

    const res: { text: string, from: { text: { autoCorrected: string } } } = await translate(input, {
        from: 'pl',
        to: 'en'
    });

    const text = res.text;

    const document = {
        content: text,
        type: 'PLAIN_TEXT',
    };

    const [result] = await client.analyzeSentiment({document: document})
    await client.close();

    result.inputSentence = input;
    result.translation = text;

    return result
}

And I have:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

In loop function in source code there are unclosed promises that can not be catched or waited for out of initialize function.

To fix my test case I need to use:

    const [result] = await client.analyzeSentiment({document: document})
    await client.close();

    await new Promise(resolve => {
        setTimeout(resolve, 500);
    });

but it is not ideal solution.

stephenplusplus commented 4 years ago

@gustawdaniel would you mind making this easier to reproduce, maybe boiling it down to the least amount of code possible? As written, there are external dependencies being used and it's not clear how to reproduce your application or testing architecture.

telpirion commented 3 years ago

Thank you for filing this issue. We asked some clarifying questions a while ago and never heard back from you. We are unable to proceed with this issue until then, so we are closing it. Please feel free to re-open this issue with more information.