Skip to content

Commit

Permalink
Added AdminClientPoolImpl for #109
Browse files Browse the repository at this point in the history
  • Loading branch information
gresrun committed Jul 6, 2016
1 parent 5483d4e commit 939c9ab
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@
<version>${jmock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/net/greghaines/jesque/admin/AdminClientPoolImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2016 Greg Haines
*
* Licensed 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 net.greghaines.jesque.admin;

import net.greghaines.jesque.Config;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

/**
* AdminClientPoolImpl publishes jobs to channels using a connection pool.
*
* @author Greg Haines
*/
public class AdminClientPoolImpl extends AbstractAdminClient {

private final Pool<Jedis> jedisPool;

/**
* Create a new AdminClientPoolImpl using the supplied configuration and connection pool.
*
* @param config used to create a connection to Redis
* @param jedisPool connection pool
*/
public AdminClientPoolImpl(final Config config, final Pool<Jedis> jedisPool) {
super(config);
if (jedisPool == null) {
throw new IllegalArgumentException("jedisPool must not be null");
}
this.jedisPool = jedisPool;
}

/**
* {@inheritDoc}
*/
@Override
protected void doPublish(final String channel, final String jobJson) {
doPublish(this.jedisPool.getResource(), getNamespace(), channel, jobJson);
}

/**
* {@inheritDoc}
*/
@Override
public void end() {
if (this.jedisPool != null && !this.jedisPool.isClosed()) {
this.jedisPool.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.greghaines.jesque.admin;

import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import net.greghaines.jesque.Config;
import net.greghaines.jesque.ConfigBuilder;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

public class TestAdminClientPoolImpl {

private static final Config CONFIG = new ConfigBuilder().build();

@Mock
private Pool<Jedis> jedisPool;
@Mock
private Jedis jedis;
private AdminClientPoolImpl adminClient;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(this.jedisPool.getResource()).thenReturn(this.jedis);
this.adminClient = new AdminClientPoolImpl(CONFIG, this.jedisPool);
}

@Test(expected=IllegalArgumentException.class)
public void testConstructor_NullConfig() {
new AdminClientPoolImpl(null, this.jedisPool);
}

@Test(expected=IllegalArgumentException.class)
public void testConstructor_NullPool() {
new AdminClientPoolImpl(CONFIG, null);
}

@Test
public void testShutdownWorkers() {
this.adminClient.shutdownWorkers(true);
verify(this.jedis).publish("resque:channel:admin",
"{\"class\":\"ShutdownCommand\",\"args\":[true],\"vars\":null}");
}

@Test
public void testEnd_Open() {
when(this.jedisPool.isClosed()).thenReturn(false);
this.adminClient.end();
verify(this.jedisPool).close();
}

@Test
public void testEnd_Closed() {
when(this.jedisPool.isClosed()).thenReturn(true);
this.adminClient.end();
verify(this.jedisPool, never()).close();
}
}

0 comments on commit 939c9ab

Please sign in to comment.