## Chapter 18. Testing Red5

## 18.1. Overview

As of version 0.8, the Red5 Testing framework has been modified and updated. The unit-tests have been updated to pass, and a automated system-testing and continuous integration framework has been added. This document attempts to explain the thoughts and architecture involved so as to facilitate review by the Red5 core team.

## 18.2. How to Start Testing Without Reading This Chapter

To see the results of the Red5 continuous build server (this URL may change), go to http://build.theyard.net/.

The build server runs after every check-in, as well as every night. We test against JDK5 and JDK6.

To run all the Red5 unit tests yourself, check out the Red5 tree, and run:

    $ ant run-tests

To see the results, open this file in a browser

doc/test/index.html

To run all the Red5 system tests, make sure you don't already have red5 running then in one terminal type:

    $ ant run-tests-server

Then open this file in a browser.&nbsp; When the security dialog comes up, give it access to your camera, and select the box to remember the setting (if you want it to auto-run in the future):

test/fixtures/red5-selftest.swf

You can find all log files generate by the red5 test server in:

bin/testcases/testreports/dist/log

You can find the documentation for what the system tests do here (and can add to it by just putting ASDoc style comments in any System tests you add):

Current Flash System Tests

And that's it.

## 18.3. Who Should Read This Chapter In Depth?

This chapter is targeted at people who are:

1. Modifying Red5 code directly and want to make sure their code works.

2. Interested in how Red5 is working to improve quality, and has some experience with software testing.

3. People who have found a bug in Red5, and want to submit a patch with a Unit Test or System Test that will catch regressions.

4. Goblins.

## 18.4. Red5 Testing Strategy

The Red5 Testing Strategy has 5 components to it:

\# | Type | Description
1 | Code | Write great code; we've done this from day one and see no reason to stop now.
2 | Unit Tests |
3 | Functional Tests | Write functional tests to simulate network interactions

This is not yet implemented. We plan to do this in the future using RTMPClient, but any help the community can give here is appreciated. ||

4 | System Tests | Use flash-based system-tests (using AsUnit) that make sure end-to-end interaction with Adobe's flash player works as expected. For example, we test that we can connect from Flash via RTMP, we can play back pre-recorded FLV files, and we can publish from a Camera.
5 | Continuous Building | Build and run all tests every time someone checks in to make sure all tests still pass. Currently the continuous server can be found here (but it will move): http://build.theyard.net/

## 18.5. Red5 Testing Props

Major props go to the following folks:

1. The red5 development team because, well, test frameworks don't mean shit if you don't got good stuff to test.

2. Thijs, who set up the initial Red5 build server, and showed me how to get get Apache and Tomcat working nicely together.

## 18.6. Unit Testing

### 18.6.1. Purpose

The purpose of a unit test is to make sure a java object operates according to its specification, regardless of how it is plugged in with other objects. For a good overview of check out this Wikipedia Unit Testing web page. For example, you can (and we do) have Unit Tests that test we're encoding and decoding data from AMF codec's correctly using Mock Objects, that test whether or not we can load information from Spring, and whether or not we can inject Meta Data into FLV viles correctly.

### 18.6.2. Technology

Red5 uses the JUnit unit testing framework. If you're not familiar with that suite, please check it out. It is the de-facto standard for Java Unit Testing.

### 18.6.3. Running Tests

To run tests, checkout the latest server build, and run the following ant command:

    $ ant run-tests

To see the results, you can open the doc/test/index.html file once the tests are finished.

firefox doc/test/index.html

### 18.6.4. Creating New Tests

Writing unit tests in the JUnit framework is beyond the scope of this document, but you can find help at the JUnit site. The Red5 unit test framework support JUnit 4.0, but can run JUnit 3.x-style tests as well. To create a new unit test, just create a new JUnitclass source file to the right path under test, and end the source filename with the string "Test.java". For example, to test org.red5.server.api.ANewClass, you would create the following java file under the "test" directory:

test/org/red5/server/api/ANewClassTest.java

Once you do that, the compile process should pick up your new file and run the tests automatically.&nbsp;

By default, the run-tests runs all unit tests in the following directory:

bin/testcases/testreports

### 18.6.5. Running unit tests from eclipse

In theory every should work, but you may need to set the directory eclipse runs the test from. Make sure it is set to:

bin/testcases/testreports

### 18.6.6. Guidelines for New Unit Tests

Unit tests help make the code base stronger, but that said we do need to make sure that unit tests meet certain guidelines so we can have a useful build process. Those guidelines are:

- Unit Tests MUST be self-contained (i.e. each test should run independently).

- Unit Tests MUST not require a Red5 server to be running.

- Unit Tests MAY assume that no Red5 instance is currently running while they run (and so may fire up Red5 objects that bind to ports if appropriate).

- Unit Tests MUST NOT require thread-timing specific to your machine to run (or fail) consistently.

- Unit Tests MUST run successfully 100% in order to be checked in. That means, no checking in tests that fail with "not implemented".

- Unit Tests SHOULD try to avoid introducing new dependencies, but if you must use one (for example http://multithreadedtc.googlecode.com/ can be useful), identify it when you submit a JUnit test case and we'll review whether or not to add to the ivy test dependencies.

- Unit Tests MUST document what they do, and how to tell if they really worked, in JavaDoc comments above each test method.

- Unit Tests SHOULD be written using JUnit 4.x annotations

### 18.6.7. Submitting New Unit Tests

We really want new Unit Tests, so if you have a Unit Test that meets the above guidelines we'd love to consider it. To submit it, do the following:

- Review the guidelines above. Really.

- If you're fixing a bug:

  - Create a unit test and make sure it fails 100% of the time before you fix the bug.

  - Fix the bug.

  - Ensure that unit test succeeds 100% of the time after you fix the bug.

- If you're testing a new feature:

  - Write your new feature.

  - Create a diff file with your patch using "svn diff" from tip of tree.

  - Ensure that unit test succeeds 100% of the time after you fix the bug.

- File a new issue in Trac in the "Developer Tools" section. Please attach the diff files and the contents of any new files, and specify the following:

  - What the test tests.

  - Brief overview of how it works.

- Someone (probably Art) will review it and get back to you on changes that may be needed, or will commit it.

### 18.6.8. Suggesting New Unit Tests

We really do want new unit tests, and would love suggestions. But bear in mind that Red5 is a 100% volunteer project and most people who work on it have full time day jobs (their time spent on red5 is a labor of love). So don't be hurt if your suggestion for a test is not picked up on.

That said, a great way to suggest an area to test is to go ahead and write the system test yourself\! Send it to the list, and it'll probably get a warm reception.

## 18.7. Integration Testing

We currently don't have a integration testing framework, but when I next return to this area, I'll try adding one. The basic idea for this (which I love) is to make a framework based on the RTMPClient.

### 18.7.1. Purpose

The purpose of integration testing is to start to plug together different simpler modules (that hopefully have been unit tested) to see if they play nice together. See this Integration Testing Wikipedia Pagefor an overview of the concepts.

## 18.8. System Testing

### 18.8.1. Purpose

When all is set and done, and you've Unit tested everything, and did integration testing by mixing together different components, you're still not done. At some point, a user is going to pick up your application, and start using it. And if you haven't tested from that end to your code and back, well chances are something will break.

That's where System Testing comes in. In the System Test we try to do some basic end-to-end tests to see if our code performs as expected from the end-user's perspective.

## 18.9. Technology

For Flash system testing (a.k.a selftest), we use the ASUnit|http://asunit.org/ framework, which is very similar to JUnit. You can find the current Flash self test here.

We also use The Yard Flash Libraries to abstract away some components of Flash connecting and stream playing, but you're not required to use those libraries if you submit new flash unit tests.

## 18.10. Running Tests

The Red5 system tests require a special test server to be running. This is just a mostly empty red5 server with one special application installed:

http://localhost/selftest

That selftest Red5 application has the following service exposed under the name "echo":

red5.server.services.IEchoService.java:
[http://code.google.com/p/red5/source/browse/java/server/trunk/test/org/red5/server/service/IEchoService.java]

Every method on that Java Interface is callable from Flash by using the prefix "echo.".&nbsp; For example, "echo.echoNumber" will call the echoNumber method over RTMP/AMF.&nbsp;

There are two ways to run the system test:

Attended - Run a Test Server

    $ ant run-tests-server

Start up the Flash Self Test application

flashplayer tests/fixtures/red5-selftest.swf

This method assumes someone is watching the test.

Unattended -

    $ ant run-tests-systemtest

This only works on Linux. It starts up a red5 server, runs the system test in the background, and then collects all log artifacts in the directory "output" relative to the current directory. It will also take snapshop pictures of the desktop as running if ImageMagick's import tool is installed. ||

System tests run the server with RED5_HOME set to bin/testcases/testreports/dist, and runs the flash clients from the directory bin/testcases/testreports/fixtures.

Lastly, you should ensure red5 is not currently running on the server you run a system test on. However, because the system tests use their own version of Red5, you don't need to worry about them clobbering anything in your own Red5 installation.

The System Tests use a series of scripts located in:

test/scripts

to automatically start-up and shutdown red5, as well as find the necessary flash logs from different parts of the system. The main one of interest is:

test/scripts/red5-flash-player-headless

It assumes it's running under a Windowing system (e.g. XWindows) with a Bourne Shell, and then starts a clean red5 server, runs the Flash system tests, and cleans up afterwards.

## 18.11. Creating New Tests

Writing unit tests in theAsUnit framework is beyond the scope of this document, but you can find help at the AsUnit site.

But to create a new system test, you can start with the Flash selftest application:svn checkout http://red5.googlecode.com/svn/flash/trunk/selftest red5_selftestTo create a new AsUnit test just create a newclass source file to the right path under test, and end the source filename with the string "Test.as". For example:

test/org/red5/server/decodingComplexObjectOverAMFTest3.as

Once you do that, you'll need to modify the AllTests.as file in the directory to add your new test.

We use The Yard flash libraries|http://code.google.com/p/theyard/ to abstract away some of the complexities of connecting to and manipulating NetConnection and NetStream objects. See the Yard flash library documentation. You don't have to use them for new tests, but they can make things a lot easier (for example, by taking care of connecting for you).

To see documentation of existing tests, run:

ant doc

## 18.12. A Sample System Test

Here's a very straightforward System Test submitted by trebor (at) vlideshow.com.

This test connects to the test server and calls the "echo.echoString" method to pass a String to Red5, and then make sure we get the same array back.&nbsp; It tests both AMF0 and AMF3 using the same code path because they should be the same.

EchoStringTest.as: AMF0 and AMF3 Strings sent over RTMP Test
[http://code.google.com/p/red5/source/browse/flash/trunk/selftest/test/src/org/red5/server/io/EchoStringTest.as]

## 18.13. Guidelines for New System Tests

Unfortunately Flash ActionScript is not as forgiving as Java is about cleaning up after a test is finished, so the guidelines for writing System Tests are somewhat more involved.&nbsp; Also, these tests MUST be runnable in a "unattended" mode \-\- meaning requiring no human interaction, to the bar is higher.

- System Tests MUST not require any human interaction \-\- i.e. if a human can't give permission for something, it should fail without blocking.

- System Tests MAY draw on the flash screen but MUST remove any artifacts when done

- System Tests MUST be self-contained (i.e. each test should run independently).

- System Tests MAY assume that a Red5 instance is running on localhost, on port 1935, and that the selftest application is available.

- System Tests MAY assume that the selftest application has the Echo service installed.

- System Tests MUST clean up fully after themselves. That is, they must disconnect and remove any event handlers..

- System Tests MUST run successfully 100% in order to be checked in. That means, no checking in tests that fail with "not implemented".

- System Tests SHOULD try to avoid introducing new dependencies, but if you must use one (for example http://theyard.googlecode.com/ can be useful), identify it when you submit a test case and we'll review whether or not to add to the ivy test dependencies.

- System Tests MUST document what they do, and how to tell if they really worked, in AsDoc comments above each test method.

## 18.14. Submitting New System Tests

We really want new System Tests, so if you have a System Test that meets the above guidelines we'd love to consider it. To submit it, do the following:

- Review the guidelines above. Really.

- If you're fixing a bug:

** Create a system test and make sure it fails 100% of the time before you fix the bug. ** Fix the bug, and run a new test server. ** Ensure that unit test succeeds 100% of the time after you fix the bug.

- If you're testing a new feature:

** Write your new feature. ** Create a unit test and make it it succeeds 100% of the time with the new feature.

- Create a diff file with your patch using "svn diff" from tip of tree.

- File a new issue in Trac in the "Developer Tools" section. Please attach the diff files and the contents of any new files, and specify the following:

** What the test tests ** Brief overview of how it works

- Someone (probably Art) will review it and get back to you on changes that may be needed, or will commit it.

If your change is accepted, we'll integrate it into the Flash self-test, and update the Java Server trunk to use the new Flash selftest as our system test.

## 18.15. Suggesting New System Tests

We really do want new tests, and would love suggestions. But bear in mind that Red5 is a 100% volunteer project and most people who work on it have full time day jobs (their time spent on red5 is a labor of love). So don't be hurt if your suggestion for a test is not picked up on.

That said, a great way to suggest an area to test is to go ahead and write the system test yourself\! Send it to the list, and it'll probably get a warm reception.

## 18.16. Continuous Integration

### 18.16.1. Overview

The last step of our testing framework is to run a continuous build. See this Wikipedia Pagefor some of the principles involved.

The basic idea is to do a checkout, run all unit, functional and system tests, and then notify the person who checked in, and any others that are interested, about the current state of the build. The idea is that it is easier to fix bugs when they are introduced, than if they are found days or weeks later.

### 18.16.2. Technology

We use Hudson as our continuous build server running inside a Tomcat instance (running as the Hudson, not root, user) that is forwarded to by Apache2. This currently runs on an Amazon EC2 small instance hosted at:

[http://build.theyard.net/]

E-Mail notification of bad builds are sent to the last person who checked in, and to the red5-builds (at) googlecode.com group.

We run the following builds continuously:

- We build the java/server/trunk against JDK 1.6 on Linux i386 (Ubuntu) and run all units tests

- If this is successful, we run all system tests under JDK 1.6 on Linux i386 (Ubuntu) .

- We build the java/server/trunk against JDK 1.5 on Linux i386 (Ubuntu) and run all unit tests.

- If this is successful, we run all system tests under JDK 1.5 on Linux i386 (Ubuntu) .

### 18.16.3. How To Run The Continuous Build

The Continuous Build server will run any time you check something into the Java Server. It also runs once every night.

If you're on the Red5 dev team and want to set up new job, or log-in to hudson directly, talk to Art Clarke and he'll hook you up.

### 18.16.4. How to Submit New Jobs for Continuous Building

For now, send a request to red5devs@osflash.org and we'll evaluate it.

## 18.17. How you can help with Continuous Building

If you're willing do donate a i386 Amazon EC2 instance or an i86_64 Amazon EC2 instance, we're in need of both to do testing on. The current set up is temporary.

### 18.17.1. How to Set up a Continuous Build Server

NOTE: THIS SECTION IS MEANT FOR SERIOUSLY ADVANCED RED5 USERS. 99.999999% of people shouldn't even read this.

Glad you asked. We used an Amazon EC2 instance to get us started. Specifically this AMI from Eric Hammon at alestic.com.

We then created a script that makes that image into one that can run Red5's continuous build server. See:

http://red5.googlecode.com/svn/build/remote/trunk/ec2/

To set up an AWS EC2 instance to build and auto-test red5, do the following:

1. Learn how to use Amazon EC2.

2. Start up an instance of Ubuntu 8.04 LTS Hardy: ami-1cd73375

3. Check out the Red5 remote build branch:

    $ svn checkout http://red5.googlecode.com/build/remote/trunk/

1. Copy the ec2/ec2-explode directory to your new Amazon EC2 instance:

    cd ec2/ec2-explode
    ./ec2-implode ../ec2.tgz
    scp -i YOUR_AWS_KEYPAIR root@YOUR_AWS_PUBLIC_IP:/tmp

1. Log into your AWS EC2 instance:

    $ ssh -i YOUR_AWS_KEYPAIR -l root YOUR_AWS_PUBLIC_IP

1. Prepare your ec2-explode package:

    cd tmp
      tar xzvf ec2.tgz

1. Run the ec2-explode script:

./ec2-explode

You will have to accept the Sun JDK license, choose a password for your XAuthority file (don't worry \-\- the X ports aren't opened, you just need that to run a headless X Server to make the Flash System Tests run), and enter the data necessary to send mail from your machine.

1. You'll need to patch up your Apache 2 files to reflect your domain name:

    rename /etc/apache2/sites-enabled/build.theyard.net /etc/apache2/sites-enabled/YOUR-APACHE-SITE
    vi /etc/apache2/sites-enabled/YOUR-APACHE-SITE
    service apache2 restart

1. Go to your website and make sure hudson is running:

http://YOUR-AWS-PUBLIC-IP/

It's probably a good idea to change the default "hudson" password as well. It defaults to: 10.

fmskiller

This is the password for the Hudson UI, not the password on the hudson linux account. By default the linux hudson account doesn't allow log in using passwords.

Prev | Home | Next
Chapter 17. Red5 Demo Applications | Red5 Open Source Flash Server | Chapter 19. Plugins