title: How to test keyedbroadcastprocessfunction in flink?
body: <p>I am new to flink i am trying write junit test cases to test KeyedBroadCastProcessFunction. Below is my code ,i am currently calling the getDataStreamOutput method in TestUtils class and passing inputdata and patternrules to method once the input data is evaluated against list of pattern rules and if input data satisfy the condition i will get the signal and calling sink function and returning output data as string in getDataStreamOutput method</p>
  <pre><code> @Test
      public void testCompareInputAndOutputDataForInputSignal() throws Exception {
          Assertions.assertEquals(sampleInputSignal,
                  TestUtils.getDataStreamOutput(
                          inputSignal,
                          patternRules));
      }
  
  
  
  public static String getDataStreamOutput(JSONObject input, Map&lt;String, String&gt; patternRules) throws Exception {
  
              env.setParallelism(1);
  
              DataStream&lt;JSONObject&gt; inputSignal = env.fromElements(input);
  
              DataStream&lt;Map&lt;String, String&gt;&gt; rawPatternStream =
                      env.fromElements(patternRules);
  
              //Generate a key,value pair of set of patterns where key is pattern name and value is pattern condition
              DataStream&lt;Tuple2&lt;String, Map&lt;String, String&gt;&gt;&gt; patternRuleStream =
                      rawPatternStream.flatMap(new FlatMapFunction&lt;Map&lt;String, String&gt;,
                              Tuple2&lt;String, Map&lt;String, String&gt;&gt;&gt;() {
                          @Override
                          public void flatMap(Map&lt;String, String&gt; patternRules,
                                              Collector&lt;Tuple2&lt;String, Map&lt;String, String&gt;&gt;&gt; out) throws Exception {
                              for (Map.Entry&lt;String, String&gt; stringEntry : patternRules.entrySet()) {
                                  JSONObject jsonObject = new JSONObject(stringEntry.getValue());
                                  Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
                                  for (String key : jsonObject.keySet()) {
                                      String value = jsonObject.get(key).toString();
                                      map.put(key, value);
                                  }
                                  out.collect(new Tuple2&lt;&gt;(stringEntry.getKey(), map));
                              }
                          }
                      });
  
              BroadcastStream&lt;Tuple2&lt;String, Map&lt;String, String&gt;&gt;&gt; patternRuleBroadcast =
                      patternStream.broadcast(patternRuleDescriptor);
  
  
              DataStream&lt;Tuple2&lt;String, JSONObject&gt;&gt; validSignal = inputSignal.map(new MapFunction&lt;JSONObject,
                      Tuple2&lt;String, JSONObject&gt;&gt;() {
                  @Override
                  public Tuple2&lt;String, JSONObject&gt; map(JSONObject inputSignal) throws Exception {
                      String source =
                              inputSignal.getSource();
                      return new Tuple2&lt;&gt;(source, inputSignal);
                  }
              }).keyBy(0).connect(patternRuleBroadcast).process(new MyKeyedBroadCastProcessFunction());
              
              
               validSignal.map(new MapFunction&lt;Tuple2&lt;String, JSONObject&gt;,
                      JSONObject&gt;() {
                  @Override
                  public JSONObject map(Tuple2&lt;String, JSONObject&gt; inputSignal) throws Exception {
                      return inputSignal.f1;
                  }
              }).addSink(new getDataStreamOutput());
  
              env.execute(&quot;TestFlink&quot;);
          }
          return (getDataStreamOutput.dataStreamOutput);
      }
  
  
      @SuppressWarnings(&quot;serial&quot;)
      public static final class getDataStreamOutput implements SinkFunction&lt;JSONObject&gt; {
          public static String dataStreamOutput;
  
          public void invoke(JSONObject inputSignal) throws Exception {
              dataStreamOutput = inputSignal.toString();
          }
      }
  </code></pre>
  <p>I need to test different inputs with same broadcast rules but each time i am calling this function its again and again doing process from beginning take input signal broadcast data, is there a way i can broadcast once and keeping on sending the input to the method i explored i can use CoFlatMapFunction something like below to combine datastream and keep on sending the input rules while method is running but for this one of the datastream has to keep on getting data from kafka topic again it will overburden on method to load kafka utils and server</p>
  <pre><code> DataStream&lt;JSONObject&gt; inputSignalFromKafka = env.addSource(inputSignalKafka);
  
      DataStream&lt;org.json.JSONObject&gt; inputSignalFromMethod = env.fromElements(inputSignal));
      
      DataStream&lt;JSONObject&gt; inputSignal = inputSignalFromMethod.connect(inputSignalFromKafka)
                  .flatMap(new SignalCoFlatMapper());
  
  
     public static class SignalCoFlatMapper
              implements CoFlatMapFunction&lt;JSONObject, JSONObject, JSONObject&gt; {
  
          @Override
          public void flatMap1(JSONObject inputValue, Collector&lt;JSONObject&gt; out) throws Exception {
              out.collect(inputValue);
  
          }
  
          @Override
          public void flatMap2(JSONObject kafkaValue, Collector&lt;JSONObject&gt; out) throws Exception {
              out.collect(kafkaValue);
  
          }
      }
  </code></pre>
  <p>I found a link in stackoverflow <a href="https://stackoverflow.com/questions/54667508/how-to-unit-test-broadcastprocessfunction-in-flink-when-processelement-depends-o">How to unit test BroadcastProcessFunction in flink when processElement depends on broadcasted data</a> but this is confused me a lot</p>
  <p>Any way i can only broadcast only once in Before method in test cases and keeping sending different kind of data to my broadcast function</p>