MockWriteTopic

class lsst.ts.salobj.topics.MockWriteTopic(*, salinfo: SalInfo, attr_name: str, min_seq_num: int | None = 1, max_seq_num: int = 2147483647, initial_seq_num: int | None = None)

Bases: WriteTopic

A version of WriteTopic that records every message written instead of actually writing it.

It is easiest to construct these by calling make_mock_write_topics. MockWriteTopic is only intended for unit tests and related short-term use; it saves every message, so long term use will leak memory.

Parameters:
salinfoSalInfo

SAL component information

attr_namestr

Topic name with attribute prefix. The prefix must be one of: cmd_, evt_, tel_, or (only for the ackcmd topic) ack_.

min_seq_numint or None, optional

Minimum value for the private_seqNum field. The default is 1. If None then private_seqNum is not set; this is needed for the ackcmd writer, which sets the field itself.

max_seq_numint, optional

Maximum value for private_seqNum, inclusive. The default is the maximum allowed positive value (private_seqNum is a 4-byte signed int). Ignored if min_seq_num is None.

initial_seq_numint, optional

Initial sequence number; if None use min_seq_num.

Attributes:
data_listlist [type_hints.BaseMsgType]

List of data written, with newer data at the end.

Plus the attributes of:

WriteTopic

Attributes Summary

DataType

The type (class) for a message of this topic.

data

Internally cached message.

has_data

Has data ever been set?

metadata

Get topic metadata as a TopicMetadata, if available,else None.

volatile

Does this topic have volatile durability?

Methods Summary

basic_close()

A synchronous and possibly less thorough version of close.

close()

Shut down and release resources.

set(**kwargs)

Set one or more fields of message data self.data.

set_write(*[, force_output])

Set zero or more fields of self.data and write if changed or if force_output true.

write()

Write the current data and return a copy of the data written.

Attributes Documentation

DataType

The type (class) for a message of this topic.

When you read or write a message for this topic you are reading or writing an instance of DataType.

Notes

The preferred way to write a message for a topic is:

  • RemoteCommand.start to start a command.

  • CommandEvent.write to write an event.

  • CommandTelemetry.write to write a telemetry message.

data

Internally cached message.

Raises:
TypeError

If data is not an instance of DataType

Notes

Do not assume the data will be constant. You can make a copy using copy.copy(data).

has_data

Has data ever been set?

A value of true simply means that set or set_write has been called at least once since the topic was constructed. All public fields will have their default value until they are set to something else. (Private fields are automatically set when the message is written.)

metadata

Get topic metadata as a TopicMetadata, if available,else None.

volatile

Does this topic have volatile durability?

Methods Documentation

basic_close() None

A synchronous and possibly less thorough version of close.

Intended for exit handlers and constructor error handlers.

async close() None

Shut down and release resources.

Intended to be called by SalInfo.close(), since that tracks all topics.

set(**kwargs: Any) bool

Set one or more fields of message data self.data.

This is useful when accumulating data for a topic in different bits of code. Have each bit of code call set to set the fields it knows about. Have the last bit of code call set_write (with force_output=True, for events) to set the remaining fields and write the completed message. Or set all fields with set and then call write to write the message.

If you have all the information for a topic in one place, it is simpler to call set_write to set all of the fields and write the message.

Parameters:
**kwargsdict [str, any]

Dict of field name: new value for that field:

  • Any key whose value is None is checked for existence, but the value of the field is not changed.

  • If the field being set is an array then the value must either be an array of the same length or a scalar (which replaces every element of the array).

Returns:
did_changebool

True if self.data was changed, or if this was the first call to set.

Raises:
AttributeError

If the topic does not have the specified field.

ValueError

If the field cannot be set to the specified value.

Notes

If one or more fields cannot be set, the message data may be partially updated.

async set_write(*, force_output: bool | None = None, **kwargs: Any) SetWriteResult

Set zero or more fields of self.data and write if changed or if force_output true.

Parameters:
force_outputbool, optional

If false, only write the message if this call changes any of the specified fields, or if has_data is false. If true, always write the message. If None (the default), use the class default, which is:

**kwargsdict [str, any]

field_name=new_value for fields you wish to set. Unspecified fields retain their current value, which may have been set by earlier calls to set or set_write.

Returns:
resultSetWriteResult

The resulting data and some flags.

Notes

The reason there are separate set_write and write methods is that write reliably writes the message, whereas set_write may not (for details, see force_output above).

The default value for force_output is specified by class constant default_force_output.

async write() BaseMsgType

Write the current data and return a copy of the data written.

Returns:
dataself.DataType

A copy of the data that was written. This can be useful to avoid race conditions (as found in RemoteCommand).