Skip to content

Commit 50c4eef

Browse files
committed
Avoid repeated ESMF initialization in unittestTimeManagerMod.F90
ESMF was being repeatedly initialized in unittestTimeManagerMod.F90, which is an error. This led to unit tests failing silently. This commit fixes the issue by checking if ESMF is already initialized before calling ESMF_Initialize, and not calling ESMF_Finalize at the end of each unit test. Currently this skips any calls to ESMF_Finalize, as it is an error to re-call ESMF_Initialize after calling ESMF_Finalize. A better solution would be to only call ESMF_Initialize and ESMF_Finalize once per unit test executable. I am looking into whether that's possible. I'm not sure if it will cause any problems to not do the ESMF_Finalize. See #3015 (comment) for details. Resolves #3015
1 parent 8cc1d42 commit 50c4eef

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/unit_test_shr/unittestTimeManagerMod.F90

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ subroutine unittest_timemgr_setup(dtime, use_gregorian_calendar)
4848
! Should be called once for every test that uses the time manager.
4949
!
5050
! !USES:
51-
use ESMF, only : ESMF_Initialize, ESMF_SUCCESS
51+
use ESMF, only : ESMF_Initialize, ESMF_IsInitialized, ESMF_SUCCESS
5252
use clm_time_manager, only : set_timemgr_init, timemgr_init, NO_LEAP_C, GREGORIAN_C
5353
!
5454
! !ARGUMENTS:
@@ -59,6 +59,7 @@ subroutine unittest_timemgr_setup(dtime, use_gregorian_calendar)
5959
integer :: l_dtime ! local version of dtime
6060
logical :: l_use_gregorian_calendar ! local version of use_gregorian_calendar
6161
character(len=:), allocatable :: calendar
62+
logical :: esmf_is_initialized
6263
integer :: rc ! return code
6364

6465
integer, parameter :: dtime_default = 1800 ! time step (seconds)
@@ -89,9 +90,15 @@ subroutine unittest_timemgr_setup(dtime, use_gregorian_calendar)
8990
l_use_gregorian_calendar = .false.
9091
end if
9192

92-
call ESMF_Initialize(rc=rc)
93+
esmf_is_initialized = ESMF_IsInitialized(rc=rc)
9394
if (rc /= ESMF_SUCCESS) then
94-
stop 'Error in ESMF_Initialize'
95+
stop 'Error in ESMF_IsInitialized'
96+
end if
97+
if (.not. esmf_is_initialized) then
98+
call ESMF_Initialize(rc=rc)
99+
if (rc /= ESMF_SUCCESS) then
100+
stop 'Error in ESMF_Initialize'
101+
end if
95102
end if
96103

97104
if (l_use_gregorian_calendar) then
@@ -219,10 +226,12 @@ subroutine unittest_timemgr_teardown
219226

220227
call timemgr_reset()
221228

222-
call ESMF_Finalize(rc=rc)
223-
if (rc /= ESMF_SUCCESS) then
224-
stop 'Error in ESMF_Finalize'
225-
end if
229+
! If this is the end of the executable, we should call
230+
! ESMF_Finalize. But the timemgr setup and teardown routines can
231+
! be called multiple times within a single unit test executable,
232+
! and it's an error to re-call ESMF_Initialize after calling
233+
! ESMF_Finalize. So for now we just won't attempt to do an
234+
! ESMF_Finalize.
226235

227236
end subroutine unittest_timemgr_teardown
228237

0 commit comments

Comments
 (0)