/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package org.apache.geode.redis.general;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import org.apache.geode.redis.GeodeRedisServerRule;

public class ShutdownIntegrationTest {

  public Jedis jedis;
  public static int REDIS_CLIENT_TIMEOUT = 1000;

  @Rule
  public GeodeRedisServerRule server = new GeodeRedisServerRule();

  @Before
  public void setUp() {
    jedis = new Jedis("localhost", server.getPort(), REDIS_CLIENT_TIMEOUT);
  }

  @After
  public void classLevelTearDown() {
    jedis.close();
  }

  @Test
  public void shutdownActuallyShutsDownTheServer() {
    // This is the default, but it's here just to be obvious
    server.getServer().setAllowUnsupportedCommands(true);
    jedis.shutdown();

    assertThatThrownBy(() -> jedis.echo("foo")).isInstanceOf(JedisConnectionException.class);
  }

  @Test
  public void shutdownIsDisabled_whenOnlySupportedCommandsAreAllowed() {
    server.getServer().setAllowUnsupportedCommands(false);

    // Unfortunately Jedis' shutdown() doesn't seem to throw a JedisDataException when the command
    // returns an error.
    jedis.shutdown();

    assertThat(jedis.keys("*")).isEmpty();
  }
}

@@ -2140,16 +2140,28 @@
  error.%0A
+    try %7B%0A  
     jedi
@@ -2166,32 +2166,307 @@
 dis.shutdown();%0A
+    %7D catch (JedisConnectionException ignore) %7B%0A      // Sometimes it throws sometimes it does not.%0A    %7D%0A%0A    // the old jedis client may be closed by shutdown even though disabled on server%0A%0A    Jedis jedis2 = new Jedis(%22localhost%22, server.getPort(), REDIS_CLIENT_TIMEOUT);
 %0A    assertThat(
@@ -2470,16 +2470,17 @@
 at(jedis
+2
 .keys(%22*
@@ -2490,18 +2490,38 @@
 .isEmpty();%0A
+    jedis2.close();%0A
   %7D%0A%7D%0A

fix flaky ShutdownIntegrationTest (#5193)