Back to Repositories

Testing Redis Execution Persistence Integration in conductor-oss/conductor

This test suite validates the Redis implementation of Conductor’s ExecutionDAO, focusing on task persistence and workflow correlation in Redis. It ensures proper data storage and retrieval functionality for workflow execution data using Redis as the backend storage.

Test Coverage Overview

The test suite provides comprehensive coverage of Redis-based execution persistence functionality.

Key areas tested include:
  • Task creation and storage in Redis
  • Workflow-task correlation verification
  • Data persistence and retrieval operations
  • Task status management
Integration points cover Redis command execution, object mapping, and conductor property configurations.

Implementation Analysis

The testing approach utilizes Spring’s test context framework with JUnit4, implementing mock Redis commands through JedisMock.

Key patterns include:
  • Mock-based Redis command simulation
  • Spring dependency injection for ObjectMapper
  • Extension of base ExecutionDAOTest for consistency
  • Property-based configuration testing

Technical Details

Testing tools and configuration:
  • JUnit4 with SpringRunner for test execution
  • JedisMock for Redis operation simulation
  • Spring Context Configuration for dependency management
  • ObjectMapper for JSON serialization
  • Mock implementations of ConductorProperties and RedisProperties

Best Practices Demonstrated

The test suite exemplifies several testing best practices for distributed systems.

Notable practices include:
  • Proper test isolation using mock Redis implementation
  • Comprehensive assertion coverage
  • Clear test method naming conventions
  • Effective use of Spring test infrastructure
  • Proper setup and initialization patterns

conductor-oss/conductor

redis-persistence/src/test/java/com/netflix/conductor/redis/dao/RedisExecutionDAOTest.java

            
/*
 * Copyright 2022 Conductor Authors.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.netflix.conductor.redis.dao;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.netflix.conductor.common.config.TestObjectMapperConfiguration;
import com.netflix.conductor.common.metadata.tasks.TaskDef;
import com.netflix.conductor.core.config.ConductorProperties;
import com.netflix.conductor.dao.ExecutionDAO;
import com.netflix.conductor.dao.ExecutionDAOTest;
import com.netflix.conductor.model.TaskModel;
import com.netflix.conductor.redis.config.RedisProperties;
import com.netflix.conductor.redis.jedis.JedisMock;
import com.netflix.conductor.redis.jedis.JedisProxy;

import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.commands.JedisCommands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ContextConfiguration(classes = {TestObjectMapperConfiguration.class})
@RunWith(SpringRunner.class)
public class RedisExecutionDAOTest extends ExecutionDAOTest {

    private RedisExecutionDAO executionDAO;

    @Autowired private ObjectMapper objectMapper;

    @Before
    public void init() {
        ConductorProperties conductorProperties = mock(ConductorProperties.class);
        RedisProperties properties = mock(RedisProperties.class);
        when(properties.getEventExecutionPersistenceTTL()).thenReturn(Duration.ofSeconds(5));
        JedisCommands jedisMock = new JedisMock();
        JedisProxy jedisProxy = new JedisProxy(jedisMock);

        executionDAO =
                new RedisExecutionDAO(jedisProxy, objectMapper, conductorProperties, properties);
    }

    @Test
    public void testCorrelateTaskToWorkflowInDS() {
        String workflowId = "workflowId";
        String taskId = "taskId1";
        String taskDefName = "task1";

        TaskDef def = new TaskDef();
        def.setName("task1");
        def.setConcurrentExecLimit(1);

        TaskModel task = new TaskModel();
        task.setTaskId(taskId);
        task.setWorkflowInstanceId(workflowId);
        task.setReferenceTaskName("ref_name");
        task.setTaskDefName(taskDefName);
        task.setTaskType(taskDefName);
        task.setStatus(TaskModel.Status.IN_PROGRESS);
        List<TaskModel> tasks = executionDAO.createTasks(Collections.singletonList(task));
        assertNotNull(tasks);
        assertEquals(1, tasks.size());

        executionDAO.correlateTaskToWorkflowInDS(taskId, workflowId);
        tasks = executionDAO.getTasksForWorkflow(workflowId);
        assertNotNull(tasks);
        assertEquals(workflowId, tasks.get(0).getWorkflowInstanceId());
        assertEquals(taskId, tasks.get(0).getTaskId());
    }

    @Override
    protected ExecutionDAO getExecutionDAO() {
        return executionDAO;
    }
}