-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/net/greghaines/jesque/admin/AdminClientPoolImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/net/greghaines/jesque/admin/TestAdminClientPoolImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |