Sunday, 25 September 2022

Mutation test failing with executeOperation (Apollo Graphql)

I want to test my graphql queries and mutations using Jest. I am writing my unit tests as given in Apollo docs:

describe('Tests', () => {
  let testServer;

  beforeAll(() => {
    testServer = new ApolloServer({ typeDefs, resolvers });
  });

  // Below test passes 
  it('should return matching result for the movies query', async () => {
    const limit = 3;
    const query = `
              query {
                movies(limit: ${limit}) {
                  id
                  name
                }
              }
            `;

    const result = await testServer.executeOperation({ query });
    expect(result.data.movies.length).toBe(limit);
  });

  // Below test fails.
  it('should mark movie as watched using the markWatched mutation', async () => {
    const movieIds = [9873, 8754];
    const mutation = `
                  mutation {
                      markWatched(movieIds: ${movieIds}) {
                      id,
                      watched
                    }
                  }
                `;

    const result = await testServer.executeOperation({ query: mutation });
    expect(result.data.markWatched).toBe([
      {
        id: 9873,
        watched: true
      },
      {
        id: 8754,
        watched: true
      }
    ]);
  });
});

// Mutation
 type Mutation {
    markWatched(ids: [ID]!): [Movies]!
 }

// Resolver
Mutation: 
{
    markWatched: (_, { ids }) => 
        markMoviesAsWatched(ids)
}

// Data model (which is simply a local file exporting an array - moviesData in below code)
const markMoviesAsWatched = ids => {
  const response = [];
  moviesData.forEach(movie => {
    if (ids.includes(movie.id)) {
      movie.watched = true;
      response.push(movie);
    }
  });
  return response;
};

The first test regarding query is passing while the second test regarding mutation is failing with error: TypeError: Cannot read properties of undefined (reading 'markWatched'). The exact mutation runs fine in Apollo playground. Not sure what's wrong with my mutation test?



from Mutation test failing with executeOperation (Apollo Graphql)

Nginx taking way more time than uwsgi

We are running nginx and uwsgi behind Load Balancer (AWS ELB). uwsgi is able to process request in lesses than 100 ms but nginx is adding some overhead and final request time is becoming way more than uwsgi time. This is not happening for all of the requests but for only 2-3 % of requests.

Log Format

'$status [$time_local] "$request" $body_bytes_sent $request_length $request_time $upstream_response_time $upstream_connect_time $upstream_header_time $upstream_status $pipe';


200 [21/Sep/2022:11:46:46 +0000] "POST api_end_point HTTP/1.1" 7238 1546 24.848 0.036 0.000 0.036 200 .

200 [21/Sep/2022:11:46:57 +0000] "POST api_end_point HTTP/1.1" 1130 1558 2.178 0.044 0.000 0.040  200 .

200 [21/Sep/2022:11:46:56 +0000] "POST api_end_point HTTP/1.1" 1130 1565 10.212 0.028 0.000 0.024  200 .

Log 1: upstream request time is 36 ms and upstream connect time is 0 but nginx request time is 24.848 seconds.

Log 2: upstream request time is 44 ms and upstream connect time is 0 but nginx request time is 2.178 seconds.

Log 3: upstream request time is 28 ms and upstream connect time is 0 but nginx request time is 10.212 seconds.

Nginx Config:

error_log  /var/log/nginx/error.log info;
worker_processes  auto;
worker_rlimit_nofile 30000;

events {
   worker_connections  1000;
   use epoll;
   multi_accept on;
}

http {
   include       /etc/nginx/mime.types;
   default_type  application/json;
   client_max_body_size 5m;
   client_body_buffer_size      256k;
   client_header_buffer_size    1k;
   large_client_header_buffers  8 64k;
   client_header_timeout  1m;
   client_body_timeout    2m;
   send_timeout           2m;
   reset_timedout_connection on;
   server_tokens off;

   sendfile        on;
   tcp_nopush      on;
   tcp_nodelay     on;

   keepalive_timeout  200;
   keepalive_requests  2000;

   log_format mycombined '$status [$time_local] "$request" $body_bytes_sent $request_length $request_time $upstream_response_time $upstream_connect_time $upstream_header_time $upstream_status $pipe';
   map $status $loggable {
       ~^[23]  0;
       default 1;
   }

   include /etc/nginx/conf.d/*.conf;
}


server {
    listen       our_custom_port;
    server_name  localhost;
    access_log /var/log/nginx/access.log mycombined;
    location api_end_point {
         include uwsgi_params;
         uwsgi_read_timeout  5s;
         uwsgi_pass  unix:/opt/apis/server/uwsgi_socket.sock;
         uwsgi_ignore_client_abort on;
    }
    location /_health {
        include uwsgi_params;
        uwsgi_read_timeout  5s;
        uwsgi_pass  unix:/opt/apis/server/uwsgi_socket.sock;
        uwsgi_ignore_client_abort on;
   }
}

I tried using gunicorn also instead of uwsgi. Similar issue persists with nginx + gunicorn also.

Any help would be highly appreciated.



from Nginx taking way more time than uwsgi

Custom TextToSpeechService Does Not Receive Usage Hints from Google TalkBack

I'm building a custom Android TextToSpeechService which filters text by language.

Currently, I'm handling non-English-language text with my custom TextToSpeech service, and I'm sending English language text to an instance of Google Speech Services.

My problem is that, when I use Google Talkback, I'm no longer able to receive Talkback's usage hints (e.g. "Double-tap to activate", etc.)

The following code runs on a singleton which is called from my TextToSpeechService.

private TextToSpeech mAndroidTTS = null; 

public void initEnglishTTS() {
      if (mAndroidTTS == null) {
            mAndroidTTS = new TextToSpeech(mContext, i -> {
                if (i == TextToSpeech.SUCCESS) {
                    mAndroidTTS.setLanguage(Locale.US);
                }
            }, "com.google.android.tts");

        }
}

public void synthesize(String text, SynthesisCallback callback) {
      callback.start(SAMPLING_RATE_HZ,
                AudioFormat.ENCODING_PCM_16BIT, 1);
      sayInEnglish(text);  // If I comment out this line, then it works
      callback.done();

}

protected static void sayInEnglish(String text) {
        String utteranceID = UUID.randomUUID().toString();
        mAndroidTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceID);
    
}

In my custom TextToSpeechService, I have the following code:

public class CustomTtsService extends TextToSpeechService {
     // an instance of the singleton
     private SynthesizerSingleton singleton = singleton.getInstance();
     
    @Override
    protected void onSynthesizeText(SynthesisRequest request, SynthesisCallback callback) {
         String text = getRequestString(request);  
         singleton.synthesize(text, callback);
   }

   private String getRequestString(SynthesisRequest request) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return request.getCharSequenceText().toString();
        } else {
            return request.getText();
        }
    }
}

If I were to comment out the line sayInEnglish(text), then all of the expected requests are received--but obviously, no utterances are synthesized.

What could I be doing incorrectly?



from Custom TextToSpeechService Does Not Receive Usage Hints from Google TalkBack