DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable

Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable

The cooling rate calculation we added to our beer cooling solution in the previous post worked, but there was still room for refactoring to achieve better testability and architecture. In this post we took a calculation method with complex dependencies and separated calculations from the old method to the new one.

Gunnar Peipman user avatar by
Gunnar Peipman
·
Feb. 04, 16 · Tutorial
Like (3)
Save
Tweet
Share
7.20K Views

Join the DZone community and get the full member experience.

Join For Free

My previous beer IoT post introduced how to measure the cooling rate of beer. As I introduced the first calculation there, I implemented it into the code to complete our calculations. Now, it’s time to focus on the implementation and make some small improvements that clean up the code and improve the technical design.

Although we can calculate cooling rate, the calculation is not implemented very well. Here’s the current method.

private void MeasureCoolingRate(IEnumerable<Measurement<double>> temps)
{
    if (!_initialMeasurementsDone)
    {
        var ambient = temps.First(t => t.DeviceId == AmbientSensorId);
        _Ta = ambient.Value;
        _T0 = temps.First(t => t.DeviceId == BeerSensorId).Value;

        _firstMeasurementTime = ambient.Time;
        _initialMeasurementsDone = true;
        return;
    }

    var timeDelta = (temps.First().Time - _firstMeasurementTime).TotalMinutes;
    if (timeDelta >= 5)
    {
        var beer = temps.First(t => t.DeviceId == BeerSensorId);
        var d1 = _T0 - _Ta;
        var d2 = beer.Value - _Ta;
        _k = Math.Log(d1 / d2) / timeDelta;

        _kMeasurementDone = true;

        Debug.WriteLine("k = " + _k);
    }
}

We have the following problems:

  • measuring method contains external logic (measuring using two steps)

  • if we want to test the calculation we have to use some fake client

  • writing unit tests for methods like this is complicated

To solve these problems we have to separate the methods of measuring from calculations. As a result we have one method that deals with cooling rate measuring and storing logic, and a second method that calculates the cooling rate based on the given data.

Separating Calculation of Cooling Rate

Let’s focus first on the cooling rate calculation. If we want to test just the calculation, we have to move the calculation to a separate method. Let’s call this method GetCoolingRate and move it to some other class.

public static class Calc
{
    public static double GetCoolingRate(double T0, double Ta, double Tt, 
                                        double t)
    {
        var d1 = T0 - Ta;
        var d2 = Tt - Ta;

        return Math.Log(d1 / d2) / t;
    }
}

We can make this method static as it doesn’t have any dependencies with other classes in the solution. Now, we can use some test framework and write tests for this method.

[Fact]
public void TestCoolingRate()
{
    double Ta = 8;
    double T0 = 22;
    double Tt = 19;
    double t = 120;
    double k = 0.002;

    double kToTest = Math.Round(Calc.GetCoolingRate(T0, Ta, Tt, t),3);

    Assert.Equal(0.002, kToTest);
}

This code is just an illustration and doesn’t handle rounding problems the way it should in the real code. But, you get the point.

Measuring Logic

As the calculation of the cooling rate is now in a separate method, we have to modify our old method to use the new one.

private void MeasureCoolingRate(IEnumerable<Measurement<double>> temps)
{
    if (!_initialMeasurementsDone)
    {
        var ambient = temps.First(t => t.DeviceId == AmbientSensorId);
        _ta = ambient.Value;
        _t0 = temps.First(t => t.DeviceId == BeerSensorId).Value;

        _firstMeasurementTime = ambient.Time;
        _initialMeasurementsDone = true;
        return;
    }

    var timeDelta = (temps.First().Time - _firstMeasurementTime).TotalMinutes;
    if (timeDelta >= 5)
    {
        var beer = temps.First(t => t.DeviceId == BeerSensorId);

        _k = Calc.GetCoolingRate(_t0, _ta, beer.Value, timeDelta);
        _kMeasurementDone = true;

        Debug.WriteLine("k = " + _k);
    }
}

Now our old method carries out only one task–controlling the process of gathering the data for the cooling rate calculation. It’s now just a helper method, and if we need to test it, then it’s more for integration tests.

Wrapping up

It’s always a good idea to find the time to clean up your code if you have introduced something new to it. The cooling rate calculation we added to our beer cooling solution in the previous post worked, but there was still room for refactoring to achieve better testability and architecture. In this post, we took a calculation method with complex dependencies and separated calculations from the old method to the new one. As a result, we can easily test cooling rate calculation using the unit test framework of our choice.

Brewing Beer With a Raspberry Pi: Table of Contents

  1. Brewing Beer With a Raspberry Pi: Measuring Cooling Rate
  2. Brewing Beer With a Raspberry Pi: Moving to ITemperatureClient Interface
  3. Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable  
  4. Brewing Beer With Raspberry Pi: Stream Analytics
  5. Brewing Beer With Raspberry Pi: Visualizing Sensor Data  
  6. Brewing Beer With a Raspberry Pi: Reporting Measurements to Azure IoT Hub
  7. Brewing Beer With Raspberry Pi: Building a Universal Windows Application
unit test

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • GPT-3 Playground: The AI That Can Write for You
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • 9 Ways You Can Improve Security Posture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: